给定正数N ,任务是找到第一个(N + 1)斐波那契数之和。
例子:
Input: N = 3
Output: 4
Explanation:
The first 4 terms of the Fibonacci Series is {0, 1, 1, 2}. Therefore, the required sum = 0 + 1 + 1 + 2 = 4.
Input: N = 4
Output: 7
天真的方法:有关解决问题的最简单方法,请参阅本文的上一篇文章。
时间复杂度: O(N)
辅助空间: O(1)
高效的方法:可以通过以下观察和计算来优化上述方法:
令S(N)代表斐波那契数列的前N个项的和。现在,为了简单地找到S(N) ,计算第(N + 2)个斐波那契数,并从结果中减去1 。该系列的第N个项可以通过以下公式计算:
现在,可以通过(F N + 2 – 1)来计算S(N)的值。
因此,我们的想法是使用上式计算S N的值:
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the sum of
// first N + 1 fibonacci numbers
void sumFib(int N)
{
// Apply the formula
long num = (long)round(pow((sqrt(5) + 1)
/ 2.0, N + 2)
/ sqrt(5));
// Print the result
cout << (num - 1);
}
// Driver Code
int main()
{
int N = 3;
sumFib(N);
return 0;
}
// This code is contributed by Dharanendra L V.
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the sum of
// first N + 1 fibonacci numbers
public static void sumFib(int N)
{
// Apply the formula
long num = (long)Math.round(
Math.pow((Math.sqrt(5) + 1)
/ 2.0,
N + 2)
/ Math.sqrt(5));
// Print the result
System.out.println(num - 1);
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
sumFib(N);
}
}
Python3
# Python program for the above approach
import math
# Function to find the sum of
# first N + 1 fibonacci numbers
def sumFib(N):
# Apply the formula
num = round(pow((pow(5,1/2) + 1) \
/ 2.0, N + 2) \
/ pow(5,1/2));
# Prthe result
print(num - 1);
# Driver Code
if __name__ == '__main__':
N = 3;
sumFib(N);
# This code is contributed by 29AjayKumar
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the sum of
// first N + 1 fibonacci numbers
public static void sumFib(int N)
{
// Apply the formula
long num = (long)Math.Round(
Math.Pow((Math.Sqrt(5) + 1)
/ 2.0,
N + 2)
/ Math.Sqrt(5));
// Print the result
Console.WriteLine(num - 1);
}
// Driver Code
static public void Main()
{
int N = 3;
sumFib(N);
}
}
// This code is contributed by jana_sayantan.
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the sum of
// first N + 1 fibonacci numbers
void sumFib(int N)
{
// Apply the formula
double num = (1 - sqrt(5)) / 2;
long val = round(
abs(1
/ (pow(num, N + 2)
+ pow(num, N + 1)
+ pow(num, N)
+ pow(num, N - 1)))
- 1);
// Print the result
cout<
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the sum of
// first N + 1 fibonacci numbers
public static void sumFib(int N)
{
// Apply the formula
double num = (1 - Math.sqrt(5)) / 2;
long val = Math.round(
Math.abs(1
/ (Math.pow(num, N + 2)
+ Math.pow(num, N + 1)
+ Math.pow(num, N)
+ Math.pow(num, N - 1)))
- 1);
// Print the result
System.out.println(val);
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
// Function Call
sumFib(N);
}
}
C#
// C# program for the above approach
using System;
public class GFG {
// Function to find the sum of
// first N + 1 fibonacci numbers
public static void sumFib(int N)
{
// Apply the formula
double num = (1 - Math.Sqrt(5)) / 2;
double val = Math.Round(
Math.Abs(1
/ (Math.Pow(num, N + 2)
+ Math.Pow(num, N + 1)
+ Math.Pow(num, N)
+ Math.Pow(num, N - 1)))
- 1);
// Print the result
Console.WriteLine(val);
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
// Function Call
sumFib(N);
}
}
// This code is contributed by 29AjayKumar
输出:
4
时间复杂度: O(1)
辅助空间: O(1)
替代方法:请按照以下步骤解决问题:
- 第N个斐波那契数也可以使用生成函数来计算。
- 第N个斐波那契数的生成函数由下式给出:
- 因此,斐波纳契数之和的生成函数由下式给出:
- G’(X)的部分分解后, G’(X)的值由下式给出:
where,
- 因此,斐波那契数之和的公式为:
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the sum of
// first N + 1 fibonacci numbers
void sumFib(int N)
{
// Apply the formula
double num = (1 - sqrt(5)) / 2;
long val = round(
abs(1
/ (pow(num, N + 2)
+ pow(num, N + 1)
+ pow(num, N)
+ pow(num, N - 1)))
- 1);
// Print the result
cout<
Java
// Java program for the above approach
import java.io.*;
class GFG {
// Function to find the sum of
// first N + 1 fibonacci numbers
public static void sumFib(int N)
{
// Apply the formula
double num = (1 - Math.sqrt(5)) / 2;
long val = Math.round(
Math.abs(1
/ (Math.pow(num, N + 2)
+ Math.pow(num, N + 1)
+ Math.pow(num, N)
+ Math.pow(num, N - 1)))
- 1);
// Print the result
System.out.println(val);
}
// Driver Code
public static void main(String[] args)
{
int N = 3;
// Function Call
sumFib(N);
}
}
C#
// C# program for the above approach
using System;
public class GFG {
// Function to find the sum of
// first N + 1 fibonacci numbers
public static void sumFib(int N)
{
// Apply the formula
double num = (1 - Math.Sqrt(5)) / 2;
double val = Math.Round(
Math.Abs(1
/ (Math.Pow(num, N + 2)
+ Math.Pow(num, N + 1)
+ Math.Pow(num, N)
+ Math.Pow(num, N - 1)))
- 1);
// Print the result
Console.WriteLine(val);
}
// Driver Code
public static void Main(String[] args)
{
int N = 3;
// Function Call
sumFib(N);
}
}
// This code is contributed by 29AjayKumar
输出:
4
时间复杂度: O(1)
辅助空间: O(1)