给定一个正数n,找到P0 + P1 + P2 +…的值。 + Pn,其中pi表示第i个Perrin数。 Perrin的前几个数字是3、0、2、3、2、5、5、7……。
例子:
Input : 4
Output : 8
Explanation : 3 + 0 + 2 + 3
Input : 6
Output : 15
Explanation : 3 + 0 + 2 + 3 + 2 + 5
在上一篇文章中,我们介绍了Perrin数字。用数学术语来说,Perrin数的序列p(n)由递归关系定义
P(n) = P(n-2) + P(n-3) for n > 2,
with initial values
P(0) = 3, P(1) = 0, P(2) = 2.
方法1(使用第n个Perrin数的递归公式)
我们可以使用上述第n个Perrin数的递归公式简单地添加数字。
C++
// C++ program to calculate sum of Perrin Numbers
#include
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
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
输出:
49
方法2(使用直接公式)
这个想法是要找到Perrin数之和与第n个Perrin数之间的关系。
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