Skip to content
Related Articles

Related Articles

Sum of Perrin Numbers
  • Difficulty Level : Basic
  • Last Updated : 26 Mar, 2021

Given a number positive number n, find value of P0 + P1 + P2 + …. + Pn where pi indicates i’th Perrin number. First few Perrin numbers are 3, 0, 2, 3, 2, 5, 5, 7…….
Examples: 
 

Input  : 4 
Output : 8
Explanation : 3 + 0 + 2 + 3

Input  : 6
Output : 15
Explanation : 3 + 0 + 2 + 3 + 2 + 5

 

In previous post, we have introduced Perrin Numbers. 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. 

Method 1 (Using Recursive Formula of n’th Perrin Number) 
We can simply add numbers using above recursive formula of n’th Perrin Number. 
 

C++




// C++ program to calculate sum of Perrin Numbers
#include <bits/stdc++.h>
using namespace std;
 
// function for sum of first n Perrin number.
int calSum(int n)
{
    int a = 3, b = 0, c = 2;
    if (n == 0) // n=0
        return 3;
    if (n == 1) // n=1
        return 3;
    if (n == 2) // n=2
        return 5;
 
    // calculate k=5 sum of three previous step.
    int sum = 5;
 
    // Sum remaining numbers
    while (n > 2) {
        int d = a + b; // calculate next term
        sum += d;
        a = b;
        b = c;
        c = d;
        n--;
    }
 
    return sum;
}
 
// Driver code
int main()
{
    int n = 9;
    cout << calSum(n);
    return 0;
}

Java




// Java program to calculate
// sum of Perrin Numbers
import java.lang.*;
 
class GFG {
 
    // function for sum of first n Perrin number.
    static int calSum(int n)
    {
 
        int a = 3, b = 0, c = 2;
        if (n == 0) // n=0
            return 3;
        if (n == 1) // n=1
            return 3;
        if (n == 2) // n=2
            return 5;
 
        // calculate k=5 sum of three previous step.
        int sum = 5;
 
        // Sum remaining numbers
        while (n > 2) {
 
            // calculate next term
            int d = a + b;
            sum += d;
            a = b;
            b = c;
            c = d;
            n--;
        }
 
        return sum;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 9;
        System.out.print(calSum(n));
    }
}
 
// This code is contributed by Anant Agarwal.

Python3




# Python program to calculate
# sum of Perrin Numbers
 
# function for sum of first
# n Perrin number.
def calSum(n):
 
    a = 3
    b = 0
    c = 2
 
    if (n == 0):  # n = 0
        return 3
    if (n == 1):  # n = 1
        return 3
    if (n == 2):  # n = 2
        return 5
  
    # calculate k = 5 sum of
    # three previous step.
    sum = 5
  
    # Sum remaining numbers
    while (n > 2):
 
        # calculate next term
        d = a + b
        sum = sum + d
        a = b
        b = c
        c = d
        n = n-1
     
    return sum
 
# Driver code
 
n = 9
print(calSum(n))
 
# This code is contributed
# by Anant Agarwal.

C#




// C# program to calculate
// sum of Perrin Numbers
using System;
 
class GFG {
 
    // function for sum of first n Perrin number.
    static int calSum(int n)
    {
 
        int a = 3, b = 0, c = 2;
 
        if (n == 0) // n=0
            return 3;
        if (n == 1) // n=1
            return 3;
        if (n == 2) // n=2
            return 5;
 
        // calculate k=5 sum of three
        // previous step.
        int sum = 5;
 
        // Sum remaining numbers
        while (n > 2) {
 
            // calculate next term
            int d = a + b;
            sum += d;
            a = b;
            b = c;
            c = d;
            n--;
        }
 
        return sum;
    }
 
    // Driver code
    public static void Main()
    {
 
        int n = 9;
 
        Console.WriteLine(calSum(n));
    }
}
 
// This code is contributed by vt_m.

PHP




<?php
// PHP program to calculate
// sum of Perrin Numbers
 
// function for sum of
// first n Perrin number.
function calSum($n)
{
     
    $a = 3;
    $b = 0;
    $c = 2;
    if ($n == 0) // n=0
        return 3;
    if ($n == 1) // n=1
        return 3;
    if ($n == 2) // n=2
        return 5;
 
    // calculate k=5 sum of
    // three previous step.
    $sum = 5;
 
    // Sum remaining numbers
    while ($n > 2)
    {
         
        // calculate next term
        $d = $a + $b;
         
        $sum += $d;
        $a = $b;
        $b = $c;
        $c = $d;
        $n--;
    }
 
    return $sum;
}
 
    // Driver code
    $n = 9;
    echo calSum($n);
     
// This code is contributed by ajit.
?>

Javascript




<script>
 
// Javascript program to calculate
// sum of Perrin Numbers
 
    // function for sum of first n Perrin number.
    function calSum(n)
    {
   
        let a = 3, b = 0, c = 2;
        if (n == 0) // n=0
            return 3;
        if (n == 1) // n=1
            return 3;
        if (n == 2) // n=2
            return 5;
   
        // calculate k=5 sum of three previous step.
        let sum = 5;
   
        // Sum remaining numbers
        while (n > 2) {
   
            // calculate next term
            let d = a + b;
            sum += d;
            a = b;
            b = c;
            c = d;
            n--;
        }
   
        return sum;
    }
      
// Driver code   
 
           let n = 9;
        document.write(calSum(n));
         
</script>

Output: 



49

 
Method 2 (Using Direct Formula) 
The idea is to find relationship between the sum of Perrin numbers and n’th Perrin number.
 

p(i) refers to the i’th perrin number.
S(i) refers to sum of perrin numbers till p(i),

We can rewrite the relation P(n) = P(n-2) + P(n-3) 
as below :
P(n-3)    = P(n)  -  P(n-2)

Similarly,
P(n-4)    = P(n-1)  -  P(n-3)
P(n-5)    = P(n-2)  -  P(n-4)
.          .           .
.          .             .
.          .             .
P(1)      = P(4)    -  P(2)
P(0)      = P(3)    -  P(1)
-------------------------------
Adding all the equations, on left side, we have
{(n) + P(n-1) - P(1) - P(2) which is S(n-3).
Therefore,
S(n-3) = P(n) + P(n-1) - P(1) - P(2)
S(n-3) = P(n) + P(n-1) - 2
S(n)   = P(n+3) + P(n+2) - 2

In order to find S(n), we can simply calculate the (n+3)’th and (n+2) Perrin number and subtract 2 from the result.
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 mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :