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 = 9print(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. ?> |
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 DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program for Perrin numbers
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count of n digit numbers whose sum of digits equals to given sum
- Print all n-digit numbers whose sum of digits equals to given sum
- Difference between sum of the squares of first n natural numbers and square of sum
- Sum of sum of all subsets of a set formed by first N natural numbers
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Difference between Sum of Cubes and Sum of First N Natural Numbers
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count numbers whose maximum sum of distinct digit-sum is less than or equals M
- Find K numbers with sum equal to N and sum of their squares maximized
- Sum of sum-series of first N Natural numbers
- Sum of series formed by difference between product and sum of N natural numbers
- Check if a given number can be expressed as pair-sum of sum of first X natural numbers
Improved By : jit_t

