Saturday, January 11, 2025
Google search engine
HomeData Modelling & AIProgram for Perrin numbers

Program for Perrin numbers

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


Javascript




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




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


Python3




# Optimized Python3 program for
# n'th perrin number
 
# function return the
# n'th perrin number
def per(n):
     
    a = 3;
    b = 0;
    c = 2;
 
    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 -= 1
     
    return m
 
# Driver code
n = 9;
print(per(n));
     
# This code is contributed by phasing17


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


Javascript




<script>
// Optimized Javascript program for
// n'th perrin number
 
// function return the
// n'th perrin number
function 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 
{\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 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!

RELATED ARTICLES

Most Popular

Recent Comments