给定一个正数n,找到f 0 + f 1 + f 2 +…的值。 + f n其中,f i表示第i个斐波那契数。请记住,f 0 = 0,f 1 = 1,f 2 = 1,f 3 = 2,f 4 = 3,f 5 = 5,…
例子 :
Input : n = 3
Output : 4
Explanation : 0 + 1 + 1 + 2 = 4
Input : n = 4
Output : 7
Explanation : 0 + 1 + 1 + 2 + 3 = 7
方法1(O(n))
蛮力法很简单,找到所有斐波那契数直到f(n),然后将它们加起来。
C++
// C++ Program to find sum of Fibonacci numbers
#include
using namespace std;
// Computes value of first fibonacci numbers
int calculateSum(int n)
{
if (n <= 0)
return 0;
int fibo[n+1];
fibo[0] = 0, fibo[1] = 1;
// Initialize result
int sum = fibo[0] + fibo[1];
// Add remaining terms
for (int i=2; i<=n; i++)
{
fibo[i] = fibo[i-1]+fibo[i-2];
sum += fibo[i];
}
return sum;
}
// Driver program to test above function
int main()
{
int n = 4;
cout << "Sum of Fibonacci numbers is : "
<< calculateSum(n) << endl;
return 0;
}
Java
// Java Program to find
// sum of Fibonacci numbers
import java.io.*;
class GFG {
// Computes value of first
// fibonacci numbers
static int calculateSum(int n)
{
if (n <= 0)
return 0;
int fibo[]=new int[n+1];
fibo[0] = 0; fibo[1] = 1;
// Initialize result
int sum = fibo[0] + fibo[1];
// Add remaining terms
for (int i=2; i<=n; i++)
{
fibo[i] = fibo[i-1]+fibo[i-2];
sum += fibo[i];
}
return sum;
}
// Driver program to test above function
public static void main(String args[])
{
int n = 4;
System.out.println("Sum of Fibonacci" +
" numbers is : "+ calculateSum(n));
}
}
// This code is contributed by Nikita tiwari.
Python3
# Python 3 Program to find
# sum of Fibonacci numbers
# Computes value of first
# fibonacci numbers
def calculateSum(n) :
if (n <= 0) :
return 0
fibo =[0] * (n+1)
fibo[1] = 1
# Initialize result
sm = fibo[0] + fibo[1]
# Add remaining terms
for i in range(2,n+1) :
fibo[i] = fibo[i-1] + fibo[i-2]
sm = sm + fibo[i]
return sm
# Driver program to test
# above function
n = 4
print("Sum of Fibonacci numbers is : " ,
calculateSum(n))
# This code is contributed
# by Nikita tiwari.
C#
// C# Program to find
// sum of Fibonacci numbers
using System;
class GFG
{
// Computes value of first
// fibonacci numbers
static int calculateSum(int n)
{
if (n <= 0)
return 0;
int []fibo = new int[n + 1];
fibo[0] = 0; fibo[1] = 1;
// Initialize result
int sum = fibo[0] + fibo[1];
// Add remaining terms
for (int i = 2; i <= n; i++)
{
fibo[i] = fibo[i - 1] + fibo[i - 2];
sum += fibo[i];
}
return sum;
}
// Driver Code
static void Main()
{
int n = 4;
Console.WriteLine( "Sum of Fibonacci" +
" numbers is : "+
calculateSum(n));
}
}
// This code is contributed by Anuj_67
PHP
Javascript
C++
// C++ Program to find sum of Fibonacci numbers in
// O(Log n) time.
#include
using namespace std;
const int MAX = 1000;
// Create an array for memoization
int f[MAX] = {0};
// Returns n'th Fibonacci number using table f[]
int fib(int n)
{
// Base cases
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
// If fib(n) is already computed
if (f[n])
return f[n];
int k = (n & 1)? (n+1)/2 : n/2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = (n & 1)? (fib(k)*fib(k) + fib(k-1)*fib(k-1))
: (2*fib(k-1) + fib(k))*fib(k);
return f[n];
}
// Computes value of first Fibonacci numbers
int calculateSum(int n)
{
return fib(n+2) - 1;
}
// Driver program to test above function
int main()
{
int n = 4;
cout << "Sum of Fibonacci numbers is : "
<< calculateSum(n) << endl;
return 0;
}
Java] # Python 3 Program to find sum of
# Fibonacci numbers in O(Log n) time.
MAX = 1000
# Create an array for memoization
f = [0] * MAX
# Returns n'th Fibonacci number
# using table f[]
def fib(n):
n = int(n)
# Base cases
if (n == 0):
return 0
if (n == 1 or n == 2):
return (1)
# If fib(n) is already computed
if (f[n] == True):
return f[n]
k = (n+1)/2 if (n & 1) else n/2
# Applying above formula [Note value n&1
# is 1 if n is odd, else 0].
f[n] = (fib(k) * fib(k) + fib(k-1) * fib(k-1)) if (n & 1) else (2 * fib(k-1) + fib(k)) * fib(k)
return f[n]
# Computes value of first Fibonacci numbers
def calculateSum(n):
return fib(n+2) - 1
# Driver program to test above function
n = 4
print("Sum of Fibonacci numbers is :", calculateSum(n))
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# Program to find sum
// of Fibonacci numbers in
// O(Log n) time.
using System;
class GFG {
static int MAX = 1000;
// Create an array for memoization
static int []f = new int[MAX];
// Returns n'th Fibonacci
// number using table f[]
static int fib(int n)
{
for(int i = 0;i < MAX;i++)
f[i] = 0;
//Arrays.fill(f, 0);
// Base cases
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
// If fib(n) is
// already computed
if (f[n] == 1)
return f[n];
int k;
if((n & 1) == 1)
k = (n + 1) / 2 ;
else
k = n / 2;
// Applying above formula
// [Note value n&1 is 1
// if n is odd, else 0].
if((n & 1) == 1)
f[n] = (fib(k) * fib(k) + fib(k - 1)
* fib(k - 1));
else
f[n] = (2 * fib(k - 1) + fib(k)) *
fib(k);
return f[n];
}
// Computes value of first
// Fibonacci numbers
static int calculateSum(int n)
{
return fib(n + 2) - 1;
}
// Driver Code
public static void Main()
{
int n = 4;
Console.Write( "Sum of Fibonacci numbers is : "
+ calculateSum(n));
}
}
// This code is contributed by nitin mittal.
PHP
输出 :
Sum of Fibonacci numbers is : 7
方法2(O(Log n))
这个想法是找到斐波那契数之和与第n个斐波那契数之间的关系。
F(i)是第i个斐波那契数。
S(i)指直到F(i)的斐波那契数的总和,
We can rewrite the relation F(n+1) = F(n) + F(n-1) as below
F(n-1) = F(n+1) - F(n)
Similarly,
F(n-2) = F(n) - F(n-1)
. . .
. . .
. . .
F(0) = F(2) - F(1)
-------------------------------
添加所有方程式,在左侧,我们有
F(0)+ F(1)+…F(n-1),即S(n-1)。
所以,
S(n-1)= F(n + 1)– F(1)
S(n-1)= F(n + 1)– 1
S(n)= F(n + 2)– 1 —(1)
为了找到S(n),只需计算第(n + 2)个斐波那契数,然后从结果中减去1。
F(n)可以使用本文中的方法5或方法6在O(log n)的时间内求值(请参阅方法5和6)。
下面是基于此方法6的实现
C++
// C++ Program to find sum of Fibonacci numbers in
// O(Log n) time.
#include
using namespace std;
const int MAX = 1000;
// Create an array for memoization
int f[MAX] = {0};
// Returns n'th Fibonacci number using table f[]
int fib(int n)
{
// Base cases
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
// If fib(n) is already computed
if (f[n])
return f[n];
int k = (n & 1)? (n+1)/2 : n/2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = (n & 1)? (fib(k)*fib(k) + fib(k-1)*fib(k-1))
: (2*fib(k-1) + fib(k))*fib(k);
return f[n];
}
// Computes value of first Fibonacci numbers
int calculateSum(int n)
{
return fib(n+2) - 1;
}
// Driver program to test above function
int main()
{
int n = 4;
cout << "Sum of Fibonacci numbers is : "
<< calculateSum(n) << endl;
return 0;
}
# Python 3 Program to find sum of
# Fibonacci numbers in O(Log n) time.
MAX = 1000
# Create an array for memoization
f = [0] * MAX
# Returns n'th Fibonacci number
# using table f[]
def fib(n):
n = int(n)
# Base cases
if (n == 0):
return 0
if (n == 1 or n == 2):
return (1)
# If fib(n) is already computed
if (f[n] == True):
return f[n]
k = (n+1)/2 if (n & 1) else n/2
# Applying above formula [Note value n&1
# is 1 if n is odd, else 0].
f[n] = (fib(k) * fib(k) + fib(k-1) * fib(k-1)) if (n & 1) else (2 * fib(k-1) + fib(k)) * fib(k)
return f[n]
# Computes value of first Fibonacci numbers
def calculateSum(n):
return fib(n+2) - 1
# Driver program to test above function
n = 4
print("Sum of Fibonacci numbers is :", calculateSum(n))
# This code is contributed by
# Smitha Dinesh Semwal
C#
// C# Program to find sum
// of Fibonacci numbers in
// O(Log n) time.
using System;
class GFG {
static int MAX = 1000;
// Create an array for memoization
static int []f = new int[MAX];
// Returns n'th Fibonacci
// number using table f[]
static int fib(int n)
{
for(int i = 0;i < MAX;i++)
f[i] = 0;
//Arrays.fill(f, 0);
// Base cases
if (n == 0)
return 0;
if (n == 1 || n == 2)
return (f[n] = 1);
// If fib(n) is
// already computed
if (f[n] == 1)
return f[n];
int k;
if((n & 1) == 1)
k = (n + 1) / 2 ;
else
k = n / 2;
// Applying above formula
// [Note value n&1 is 1
// if n is odd, else 0].
if((n & 1) == 1)
f[n] = (fib(k) * fib(k) + fib(k - 1)
* fib(k - 1));
else
f[n] = (2 * fib(k - 1) + fib(k)) *
fib(k);
return f[n];
}
// Computes value of first
// Fibonacci numbers
static int calculateSum(int n)
{
return fib(n + 2) - 1;
}
// Driver Code
public static void Main()
{
int n = 4;
Console.Write( "Sum of Fibonacci numbers is : "
+ calculateSum(n));
}
}
// This code is contributed by nitin mittal.
的PHP
输出 :
Sum of Fibonacci numbers is : 7