Sum of Perrin Numbers
Last Updated :
20 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int calSum( int n)
{
int a = 3, b = 0, c = 2;
if (n == 0)
return 3;
if (n == 1)
return 3;
if (n == 2)
return 5;
int sum = 5;
while (n > 2) {
int d = a + b;
sum += d;
a = b;
b = c;
c = d;
n--;
}
return sum;
}
int main()
{
int n = 9;
cout << calSum(n);
return 0;
}
|
Java
import java.lang.*;
class GFG {
static int calSum( int n)
{
int a = 3 , b = 0 , c = 2 ;
if (n == 0 )
return 3 ;
if (n == 1 )
return 3 ;
if (n == 2 )
return 5 ;
int sum = 5 ;
while (n > 2 ) {
int d = a + b;
sum += d;
a = b;
b = c;
c = d;
n--;
}
return sum;
}
public static void main(String[] args)
{
int n = 9 ;
System.out.print(calSum(n));
}
}
|
Python3
def calSum(n):
a = 3
b = 0
c = 2
if (n = = 0 ):
return 3
if (n = = 1 ):
return 3
if (n = = 2 ):
return 5
sum = 5
while (n > 2 ):
d = a + b
sum = sum + d
a = b
b = c
c = d
n = n - 1
return sum
n = 9
print (calSum(n))
|
C#
using System;
class GFG {
static int calSum( int n)
{
int a = 3, b = 0, c = 2;
if (n == 0)
return 3;
if (n == 1)
return 3;
if (n == 2)
return 5;
int sum = 5;
while (n > 2) {
int d = a + b;
sum += d;
a = b;
b = c;
c = d;
n--;
}
return sum;
}
public static void Main()
{
int n = 9;
Console.WriteLine(calSum(n));
}
}
|
PHP
<?php
function calSum( $n )
{
$a = 3;
$b = 0;
$c = 2;
if ( $n == 0)
return 3;
if ( $n == 1)
return 3;
if ( $n == 2)
return 5;
$sum = 5;
while ( $n > 2)
{
$d = $a + $b ;
$sum += $d ;
$a = $b ;
$b = $c ;
$c = $d ;
$n --;
}
return $sum ;
}
$n = 9;
echo calSum( $n );
?>
|
Javascript
<script>
function calSum(n)
{
let a = 3, b = 0, c = 2;
if (n == 0)
return 3;
if (n == 1)
return 3;
if (n == 2)
return 5;
let sum = 5;
while (n > 2) {
let d = a + b;
sum += d;
a = b;
b = c;
c = d;
n--;
}
return sum;
}
let n = 9;
document.write(calSum(n));
</script>
|
Output:
49
Time complexity: O(n) since using a while loop
Space complexity: O(1) since using constant variables
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.
Share your thoughts in the comments
Please Login to comment...