给定范围[l,r] ,任务是找到和fib(l)+ fib(l + 1)+ fib(l + 2)+….. + fib(r) ,其中fib(n)是第n个斐波那契数。
例子:
Input: l = 2, r = 5
Output: 11
fib(2) + fib(3) + fib(4) + fib(5) = 1 + 2 + 3 + 5 = 11
Input: l = 4, r = 8
Output: 50
天真的方法:只需以O(r – l)时间复杂度来计算fib(l)+ fib(l + 1)+ fib(l + 2)+….. + fib(r) 。
为了在O(1)中找到fib(n) ,我们将借助黄金分割率。
斐波那契使用Binet公式的计算
fib(n) = phin – psin) / ?5
Where,
phi = (1 + sqrt(5)) / 2 which is roughly equal to 1.61803398875
psi = 1 – phi = (1 – sqrt(5)) / 2 which is roughly equal to 0.61803398875
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define ll long long int
// Function to return the nth Fibonacci number
int fib(int n)
{
double phi = (1 + sqrt(5)) / 2;
return round(pow(phi, n) / sqrt(5));
}
// Function to return the required sum
ll calculateSum(int l, int r)
{
// To store the sum
ll sum = 0;
// Calculate the sum
for (int i = l; i <= r; i++)
sum += fib(i);
return sum;
}
// Driver code
int main()
{
int l = 4, r = 8;
cout << calculateSum(l, r);
return 0;
}
Java
// Java implementation of the approach
import java.lang.Math;
class GFG
{
// Function to return the nth Fibonacci number
static int fib(int n)
{
double phi = (1 + Math.sqrt(5)) / 2;
return (int)Math.round(Math.pow(phi, n) / Math.sqrt(5));
}
// Function to return the required sum
static int calculateSum(int l, int r)
{
// To store the sum
int sum = 0;
// Calculate the sum
for (int i = l; i <= r; i++)
sum += fib(i);
return sum;
}
// Driver code
public static void main(String[] args)
{
int l = 4, r = 8;
System.out.println(calculateSum(l, r));
}
}
//This code is contributed by Code_Mech.
Python3
# Python3 implementation of the approach
# Function to return the nth
# Fibonacci number
def fib(n):
phi = ((1 + (5 ** (1 / 2))) / 2);
return round((phi ** n) / (5 ** (1 / 2)));
# Function to return the required sum
def calculateSum(l, r):
# To store the sum
sum = 0;
# Calculate the sum
for i in range(l, r + 1):
sum += fib(i);
return sum;
# Driver Code
if __name__ == '__main__':
l, r = 4, 8;
print(calculateSum(l, r));
# This code contributed by Rajput-Ji
C#
// C# implemenatation of above approach
using System;
class GFG
{
// Function to return the nth Fibonacci number
static int fib(int n)
{
double phi = (1 + Math.Sqrt(5)) / 2;
return (int)Math.Round(Math.Pow(phi, n) / Math.Sqrt(5));
}
// Function to return the required sum
static int calculateSum(int l, int r)
{
// To store the sum
int sum = 0;
// Calculate the sum
for (int i = l; i <= r; i++)
sum += fib(i);
return sum;
}
// Driver code
public static void Main()
{
int l = 4, r = 8;
Console.WriteLine(calculateSum(l, r));
}
}
/* This code contributed by PrinciRaj1992 */
PHP
Javascript
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the nth Fibonacci number
int fib(int n)
{
double phi = (1 + sqrt(5)) / 2;
return round(pow(phi, n) / sqrt(5));
}
// Function to return the required sum
int calculateSum(int l, int r)
{
// Using our deduced result
int sum = fib(r + 2) - fib(l + 1);
return sum;
}
// Driver code
int main()
{
int l = 4, r = 8;
cout << calculateSum(l, r);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the nth Fibonacci number
static int fib(int n)
{
double phi = (1 + Math.sqrt(5)) / 2;
return (int) Math.round(Math.pow(phi, n) / Math.sqrt(5));
}
// Function to return the required sum
static int calculateSum(int l, int r)
{
// Using our deduced result
int sum = fib(r + 2) - fib(l + 1);
return sum;
}
// Driver code
public static void main(String[] args)
{
int l = 4, r = 8;
System.out.println(calculateSum(l, r));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
import math
# Function to return the nth
# Fibonacci number
def fib(n):
phi = (1 + math.sqrt(5)) / 2;
return int(round(pow(phi, n) /
math.sqrt(5)));
# Function to return the required sum
def calculateSum(l, r):
# Using our deduced result
sum = fib(r + 2) - fib(l + 1);
return sum;
# Driver code
l = 4;
r = 8;
print(calculateSum(l, r));
# This code is contributed by mits
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the nth Fibonacci number
static int fib(int n)
{
double phi = (1 + Math.Sqrt(5)) / 2;
return (int) Math.Round(Math.Pow(phi, n) /
Math.Sqrt(5));
}
// Function to return the required sum
static int calculateSum(int l, int r)
{
// Using our deduced result
int sum = fib(r + 2) - fib(l + 1);
return sum;
}
// Driver code
public static void Main()
{
int l = 4, r = 8;
Console.WriteLine(calculateSum(l, r));
}
}
// This code is contributed
// by Akanksha Rai
PHP
50
高效的方法:想法是找到斐波那契数之和与第n个斐波那契数之间的关系,并使用Binet公式计算其值。
关系推论
- 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)
Adding all the equations, on left side, we have
F(0) + F(1) + … + F(n – 1) which is S(n – 1)
所以,
S(n – 1)= F(n + 1)– F(1)
S(n – 1)= F(n + 1)– 1
S(n)= F(n + 2)– 1
为了找到S(n) ,只需计算第(n + 2)个斐波那契数,然后从结果中减去1 。
所以,
S(l,r)= S(r)– S(l – 1)
S(l,r)= F(r + 2)– 1 –(F(l + 1)– 1)
S(l,r)= F(r + 2)– F(l + 1)
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to return the nth Fibonacci number
int fib(int n)
{
double phi = (1 + sqrt(5)) / 2;
return round(pow(phi, n) / sqrt(5));
}
// Function to return the required sum
int calculateSum(int l, int r)
{
// Using our deduced result
int sum = fib(r + 2) - fib(l + 1);
return sum;
}
// Driver code
int main()
{
int l = 4, r = 8;
cout << calculateSum(l, r);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to return the nth Fibonacci number
static int fib(int n)
{
double phi = (1 + Math.sqrt(5)) / 2;
return (int) Math.round(Math.pow(phi, n) / Math.sqrt(5));
}
// Function to return the required sum
static int calculateSum(int l, int r)
{
// Using our deduced result
int sum = fib(r + 2) - fib(l + 1);
return sum;
}
// Driver code
public static void main(String[] args)
{
int l = 4, r = 8;
System.out.println(calculateSum(l, r));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
import math
# Function to return the nth
# Fibonacci number
def fib(n):
phi = (1 + math.sqrt(5)) / 2;
return int(round(pow(phi, n) /
math.sqrt(5)));
# Function to return the required sum
def calculateSum(l, r):
# Using our deduced result
sum = fib(r + 2) - fib(l + 1);
return sum;
# Driver code
l = 4;
r = 8;
print(calculateSum(l, r));
# This code is contributed by mits
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to return the nth Fibonacci number
static int fib(int n)
{
double phi = (1 + Math.Sqrt(5)) / 2;
return (int) Math.Round(Math.Pow(phi, n) /
Math.Sqrt(5));
}
// Function to return the required sum
static int calculateSum(int l, int r)
{
// Using our deduced result
int sum = fib(r + 2) - fib(l + 1);
return sum;
}
// Driver code
public static void Main()
{
int l = 4, r = 8;
Console.WriteLine(calculateSum(l, r));
}
}
// This code is contributed
// by Akanksha Rai
的PHP
50
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。