Perrin numbers

Here given code implementation process.

//C Program
//Display Perrin number sequence
#include <stdio.h>

int perrin(int n)
{ 
  //Base case
  if(n < 0)
  {
    return 0;
  }
  else if(n==0)
  {
    return 3;
  }
  else if(n==1)
  {
    return 0;
  }
  else if(n==2)
  {
    return 2;
  }
  //return next parrin number
  return perrin(n - 2) + perrin(n - 3);
  

}
//print n perrin number
void print_perrin(int n)
{
 
  for (int i = 0; i < n; ++i)
  {
    printf("%d ",perrin(i) );
  }
}

int main()
{
 
  print_perrin(20);

  return 0;
}

Output

3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 119 158 209
/*
 C++ Program
 Display Perrin number sequence
*/
#include<iostream>

using namespace std;

class MyNumber {
	public:
		int perrin(int n) {
			//Base case

			if (n < 0) {
				return 0;
			} else
			if (n == 0) {
				return 3;
			} else
			if (n == 1) {
				return 0;
			} else
			if (n == 2) {
				return 2;
			}
			return this->perrin(n - 2) + this->perrin(n - 3);
		}
	//print n perrin number
	void print_perrin(int n) {
		for (int i = 0; i < n; ++i) {
			cout << " " << this->perrin(i);
		}
	}
};
int main() {
	MyNumber obj;
	obj.print_perrin(20);
	return 0;
}

Output

 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 119 158 209
/*
  Java Program
  Display Perrin number sequence
*/
public class MyNumber {
 
  public int perrin(int n)
  { 
    //Base case
    if(n < 0)
    {
      return 0;
    }
    else if(n==0)
    {
      return 3;
    }
    else if(n==1)
    {
      return 0;
    }
    else if(n==2)
    {
      return 2;
    }
    //return next parrin number
    return perrin(n - 2) + perrin(n - 3);
    

  }
  //print n perrin number
  public void print_perrin(int n)
  {
   
    for (int i = 0; i < n; ++i)
    {
      System.out.print("  "+perrin(i) );
    }
  }

  public static void main(String[] args) {

    MyNumber obj = new MyNumber();

    obj.print_perrin(20);
  }
}

Output

 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 119 158 209
/*
  C# Program
  Display Perrin number sequence
*/
using System;
public class MyNumber {

	public int perrin(int n) {
		//Base case
		if (n < 0) {
			return 0;
		} else if (n == 0) {
			return 3;
		} else if (n == 1) {
			return 0;
		} else if (n == 2) {
			return 2;
		}
		//return next parrin number
		return perrin(n - 2) + perrin(n - 3);


	}
	//print n perrin number
	public void print_perrin(int n) {

		for (int i = 0; i < n; ++i) {
			Console.Write("  " + perrin(i));
		}
	}

	public static void Main(String[] args) {

		MyNumber obj = new MyNumber();

		obj.print_perrin(20);
	}
}

Output

  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209
<?php
/*
  Php Program
  Display Perrin number sequence
*/
class MyNumber {
	public 	function perrin($n) {
		//Base case

		if ($n < 0) {
			return 0;
		} else
		if ($n == 0) {
			return 3;
		} else
		if ($n == 1) {
			return 0;
		} else
		if ($n == 2) {
			return 2;
		}
		return $this->perrin($n - 2) + $this->perrin($n - 3);
	}
	//print n perrin number

	public 	function print_perrin($n) {
		for ($i = 0; $i < $n; ++$i) {
			echo(" ". $this->perrin($i));
		}
	}
};

function main() {
	$obj = new MyNumber();
	$obj->print_perrin(20);
}
main();

Output

 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 119 158 209
/*
 Node Js Program
 Display Perrin number sequence
*/
class MyNumber {
	perrin(n) {
		//Base case

		if (n < 0) {
			return 0;
		} else
		if (n == 0) {
			return 3;
		} else
		if (n == 1) {
			return 0;
		} else
		if (n == 2) {
			return 2;
		}
		return this.perrin(n - 2) + this.perrin(n - 3);
	}
	//print n perrin number
	print_perrin(n) {
		for (var i = 0; i < n; ++i) {
			process.stdout.write(" " + this.perrin(i));
		}
	}
}

function main(args) {
	var obj = new MyNumber();
	obj.print_perrin(20);
}
main();

Output

 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 119 158 209
# Python 3 Program
# Display Perrin number sequence
class MyNumber :
	def perrin(self, n) :
		# Base case

		if (n < 0) :
			return 0
		elif (n == 0) :
			return 3
		elif (n == 1) :
			return 0
		elif (n == 2) :
			return 2
		
		return self.perrin(n - 2) + self.perrin(n - 3)
	
	# print n perrin number
	def print_perrin(self, n) :
		i = 0
		while (i < n) :
			print(" ", self.perrin(i),end="")
			i += 1
		
	

def main() :
	obj = MyNumber()
	obj.print_perrin(20)


if __name__ == "__main__":
	main()

Output

 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 119 158 209
# Ruby Program 
# Display Perrin number sequence
class MyNumber 
	def perrin(n) 
		# Base case

		if (n < 0) 
			return 0
		elsif (n == 0) 
			return 3
		elsif (n == 1) 
			return 0
		elsif (n == 2) 
			return 2
		end
		return self.perrin(n - 2) + self.perrin(n - 3)
	end
	# print n perrin number
	def print_perrin(n) 
		i = 0
		while (i < n) 
			print(" ", self.perrin(i))
			i += 1
		end
	end
end
def main() 
	obj = MyNumber.new()
	obj.print_perrin(20)
end
main()

Output

 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 119 158 209
/*
 Scala Program
 Display Perrin number sequence
*/
class MyNumber {
	def perrin(n: Int): Int = {
		//Base case

		if (n < 0) {
			return 0;
		} else
		if (n == 0) {
			return 3;
		} else
		if (n == 1) {
			return 0;
		} else
		if (n == 2) {
			return 2;
		}
		return this.perrin(n - 2) + this.perrin(n - 3);
	}
	//print n perrin number
	def print_perrin(n: Int): Unit = {
		var i: Int = 0;
		while (i < n) {
			print(" " + this.perrin(i));
			i += 1;
		}
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
  		obj.print_perrin(20);
	}
}

Output

 3 0 2 3 2 5 5 7 10 12 17 22 29 39 51 68 90 119 158 209
/*
  Swift 4 Program
  Display Perrin number sequence
*/
class MyNumber {
	func perrin(_ n: Int) -> Int {
		//Base case

		if (n < 0) {
			return 0;
		} else
		if (n == 0) {
			return 3;
		} else
		if (n == 1) {
			return 0;
		} else
		if (n == 2) {
			return 2;
		}
		return self.perrin(n - 2) + self.perrin(n - 3);
	}
	//print n perrin number
	func print_perrin(_ n: Int) {
		var i: Int = 0;
		while (i < n) {
			print(" ", self.perrin(i),terminator:"");
			i += 1;
		}
	}
}
func main() {
	let obj: MyNumber = MyNumber();
	obj.print_perrin(20);
}
main();

Output

  3  0  2  3  2  5  5  7  10  12  17  22  29  39  51  68  90  119  158  209


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved