Skip to content
Related Articles

Related Articles

Program for Perrin numbers
  • Difficulty Level : Basic
  • Last Updated : 25 Jan, 2019

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>
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 code
int main()
{
    int n = 9;
    cout << per(n);
    return 0;
}
  
// This code is contributed 
// by Akanksha Rai


C




// 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 code
int main()
{
    int n = 9;
    printf("%d", per(n));
    return 0;
}


Java




// 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




# Python3 code for n'th perrin 
# number using Recursion'
  
# function return n'th
# perrin number
def 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;
print(per(n));
      
# This code is contributed mits


C#




// 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
// PHP code for n'th perrin 
// number using Recursion'
  
// function return n'th
// perrin number
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
    $n = 9;
    echo per($n);
      
#This code is contributed ajit.
?>



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++




// 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 code
int main()
{
    int n = 9;
    cout << per(n);
    return 0;
}
  
// This code is contributed 
// by Akanksha Rai


C




// 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 code
int main()
{
    int n = 9;
    printf("%d", per(n));
    return 0;
}


Java




// Optimized Java program for n'th perrin number
import 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.


C#




// Optimized C# program for n'th perrin number
using 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




<?php
// Optimized PHP program for 
// n'th perrin number
  
// function return the 
// n'th perrin number
function 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
?>



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

{\Huge \begin{pmatrix}  0& 1 & 0\\   0&  0&1 \\  1 &1  & 0 \\ \end{pmatrix}^n \begin{pmatrix} 3\\  0\\  2 \end{pmatrix} = \begin{pmatrix} P(n)\\  P(n+1)\\  P(n+2) \end{pmatrix}}

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 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.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :