The Perrin numbers are the numbers in the following integer sequence. 3, 0, 2, 3, 2, 5, 5, 7, 10, 12, 17, 22, 29, 39 … In mathematical terms, the sequence p(n) of Perrin numbers is defined by the recurrence relation
P(n) = P(n-2) + P(n-3) for n > 2,
with initial values
P(0) = 3, P(1) = 0, P(2) = 2.
Write a function int per(int n) that returns p(n). For example, if n = 0, then per() should return 3. If n = 1, then it should return 0 If n = 2, then it should return 2. For n > 2, it should return p(n-2) + p(n-3)
Method 1 ( Use recursion : Exponential ) Below is simple recursive implementation of above formula.
C++
// n'th perrin number using Recursion'
#include <bits/stdc++.h>
usingnamespacestd;
intper(intn)
{
if(n == 0)
return3;
if(n == 1)
return0;
if(n == 2)
return2;
returnper(n - 2) + per(n - 3);
}
// Driver code
intmain()
{
intn = 9;
cout << per(n);
return0;
}
// This code is contributed
// by Akanksha Rai
C
// n'th perrin number using Recursion'
#include <stdio.h>
intper(intn)
{
if(n == 0)
return3;
if(n == 1)
return0;
if(n == 2)
return2;
returnper(n - 2) + per(n - 3);
}
// Driver code
intmain()
{
intn = 9;
printf("%d", per(n));
return0;
}
Java
// Java code for n'th perrin number
// using Recursion'
importjava.io.*;
classGFG {
staticintper(intn)
{
if(n == 0)
return3;
if(n == 1)
return0;
if(n == 2)
return2;
returnper(n - 2) + per(n - 3);
}
// Driver code
publicstaticvoidmain(String[] args)
{
intn = 9;
System.out.println(per(n));
}
}
// This code is contributed by vt_m.
Python3
# Python3 code for n'th perrin
# number using Recursion'
# function return n'th
# perrin number
defper(n):
if(n ==0):
return3;
if(n ==1):
return0;
if(n ==2):
return2;
returnper(n -2) +per(n -3);
# Driver Code
n =9;
print(per(n));
# This code is contributed mits
C#
// C# code for n'th perrin number
// using Recursion'
usingSystem;
classGFG {
staticintper(intn)
{
if(n == 0)
return3;
if(n == 1)
return0;
if(n == 2)
return2;
returnper(n - 2) + per(n - 3);
}
// Driver code
publicstaticvoidMain()
{
intn = 9;
Console.Write(per(n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP code for n'th perrin
// number using Recursion'
// function return n'th
// perrin number
functionper($n)
{
if($n== 0)
return3;
if($n== 1)
return0;
if($n== 2)
return2;
returnper($n- 2) +
per($n- 3);
}
// Driver Code
$n= 9;
echoper($n);
#This code is contributed ajit.
?>
Javascript
<script>
// Javascript code for n'th perrin number
// using Recursion'
functionper(n)
{
if(n == 0)
return3;
if(n == 1)
return0;
if(n == 2)
return2;
returnper(n - 2) + per(n - 3);
}
// Driver code
let n = 9;
document.write(per(n));
</script>
Output:
12
We see that in this implementation a lot of repeated work in the following recursion tree.
Time Complexity : O(n) Auxiliary Space : O(1) Method 3: (Further Optimized : Logarithmic) We can further optimize using Matrix Exponentiation. The matrix power formula for n’th Perrin number is
We can implement this method similar to implementation of method 5 of Fibonacci numbers. Since we can compute n’th power of a constant matrix in O(Log n), time complexity of this method is O(Log n) Application : The number of different maximal independent sets in an n-vertex cycle graph is counted by the nth Perrin number for n > 1 Related Article : Sum of Perrin Numbers Reference: https://en.wikipedia.org/wiki/Perrin_number This article is contributed by DANISH_RAZA. If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!