Program for Perrin numbers

20 Apr, 2021
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++
C
Java
Python3
C#
PHP
Javascript
// n'th perrin number using Recursion'#include <bits/stdc++.h>using namespace std; int per(int n){    if (n == 0)        return 3;    if (n == 1)        return 0;    if (n == 2)        return 2;    return per(n - 2) + per(n - 3);} // Driver codeint main(){    int n = 9;    cout << per(n);    return 0;} // This code is contributed// by Akanksha Rai
// n'th perrin number using Recursion'#include <stdio.h>int per(int n){    if (n == 0)        return 3;    if (n == 1)        return 0;    if (n == 2)        return 2;    return per(n - 2) + per(n - 3);} // Driver codeint main(){    int n = 9;    printf("%d", per(n));    return 0;}
// Java code for n'th perrin number// using Recursion'import java.io.*; class GFG {     static int per(int n)    {        if (n == 0)            return 3;        if (n == 1)            return 0;        if (n == 2)            return 2;        return per(n - 2) + per(n - 3);    }     // Driver code    public static void main(String[] args)    {         int n = 9;         System.out.println(per(n));    }} // This code is contributed by vt_m.
# Python3 code for n'th perrin# number using Recursion' # function return n'th# perrin numberdef per(n):     if (n == 0):        return 3;    if (n == 1):        return 0;    if (n == 2):        return 2;    return per(n - 2) + per(n - 3); # Driver Coden = 9;print(per(n));     # This code is contributed mits
// C# code for n'th perrin number// using Recursion'using System; class GFG {     static int per(int n)    {        if (n == 0)            return 3;        if (n == 1)            return 0;        if (n == 2)            return 2;        return per(n - 2) + per(n - 3);    }     // Driver code    public static void Main()    {         int n = 9;         Console.Write(per(n));    }} // This code is contributed by vt_m.
<?php// PHP code for n'th perrin// number using Recursion' // function return n'th// perrin numberfunction per($n){    if ($n == 0)        return 3;    if ($n == 1)        return 0;    if ($n == 2)        return 2;    return per($n - 2) +           per($n - 3);}     // Driver Code    $n = 9;    echo per($n);     #This code is contributed ajit.?>
<script> // Javascript code for n'th perrin number// using Recursion'     function per(n)    {        if (n == 0)            return 3;        if (n == 1)            return 0;        if (n == 2)            return 2;        return per(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.  
                           per(8)   
                       /           \     
               per(6)             per(5)   
              /      \             /     \
        per(4)      per(3)        per(3)    per(2)
       /     \        /    \        /  \  
   per(2)   per(1)  per(1) per(0) per(1) per(0)
Method 2: ( Optimized : Linear)  
C++
C
Java
C#
PHP
Javascript
// Optimized C++ program for n'th perrin number#include <bits/stdc++.h>using namespace std;int per(int n){    int a = 3, b = 0, c = 2, i;    int m;    if (n == 0)        return a;    if (n == 1)        return b;    if (n == 2)        return c;    while (n > 2) {        m = a + b;        a = b;        b = c;        c = m;        n--;    }    return m;} // Driver codeint main(){    int n = 9;    cout << per(n);    return 0;} // This code is contributed// by Akanksha Rai
// Optimized C program for n'th perrin number#include <stdio.h>int per(int n){    int a = 3, b = 0, c = 2, i;    int m;    if (n == 0)        return a;    if (n == 1)        return b;    if (n == 2)        return c;    while (n > 2) {        m = a + b;        a = b;        b = c;        c = m;        n--;    }    return m;} // Driver codeint main(){    int n = 9;    printf("%d", per(n));    return 0;}
// Optimized Java program for n'th perrin numberimport java.io.*; class GFG {     static int per(int n)    {        int a = 3, b = 0, c = 2, i;        int m = 0;        if (n == 0)            return a;        if (n == 1)            return b;        if (n == 2)            return c;        while (n > 2) {            m = a + b;            a = b;            b = c;            c = m;            n--;        }        return m;    }     // Driver code    public static void main(String[] args)    {        int n = 9;         System.out.println(per(n));    }} // This code is contributed by vt_m.
// Optimized C# program for n'th perrin numberusing System; class GFG {     static int per(int n)    {        int a = 3, b = 0, c = 2;         // int i;        int m = 0;        if (n == 0)            return a;        if (n == 1)            return b;        if (n == 2)            return c;         while (n > 2) {            m = a + b;            a = b;            b = c;            c = m;            n--;        }         return m;    }     // Driver code    public static void Main()    {         int n = 9;         Console.WriteLine(per(n));    }} // This code is contributed by vt_m.
<?php// Optimized PHP program for// n'th perrin number // function return the// n'th perrin numberfunction per($n){    $a = 3; $b = 0;    $c = 2; $i;    $m;    if ($n == 0)        return $a;    if ($n == 1)        return $b;    if ($n == 2)        return $c;    while ($n > 2)    {        $m = $a + $b;        $a = $b;        $b = $c;        $c = $m;        $n--;    }    return $m;}     // Driver code    $n = 9;    echo per($n);     // This code is contributed by ajit?>
<script>// Optimized Javascript program for// n'th perrin number // function return the// n'th perrin numberfunction per(n){    let a = 3;    let b = 0;    let c = 2;    let i;    let m;     if (n == 0)        return a;    if (n == 1)        return b;    if (n == 2)        return c;    while (n > 2)    {        m = a + b;        a = b;        b = c;        c = m;        n--;    }    return m;}     // Driver code    n = 9;    document.write(per(n));     // This code is contributed by _saurabh_jaiswal</script>
Output:  
12
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 > 1Related Article : Sum of Perrin NumbersReference: https://en.wikipedia.org/wiki/Perrin_numberThis article is contributed by DANISH_RAZA. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks 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. 
jit_t
Mithun Kumar
Akanksha_Rai
splevel62
_saurabh_jaiswal
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number
Merge two sorted arrays with O(1) extra space