给定一个正整数N。任务是找到所有斐波那契数的平方和,直至第N个斐波那契数。那是,
f02 + f12 + f22+.......+fn2
where fi indicates i-th fibonacci number.
斐波那契数:对于所有i> = 2,f 0 = 0和f 1 = 1且f i = f i-1 + f i- 2。
例子:
Input: N = 3
Output: 6
Explanation: 0 + 1 + 1 + 4 = 6
Input: N = 6
Output: 104
Explanation: 0 + 1 + 1 + 4 + 9 + 25 + 64 = 104
方法1:找出直到N的所有斐波那契数,然后求和它们的平方。该方法将花费O(n)的时间复杂度。
以下是此方法的实现:
C++
// C++ Program to find sum of
// squares of Fibonacci numbers
#include
using namespace std;
// Function to calculate sum of
// squares of Fibonacci numbers
int calculateSquareSum(int n)
{
if (n <= 0)
return 0;
int fibo[n + 1];
fibo[0] = 0, fibo[1] = 1;
// Initialize result
int sum = (fibo[0] * fibo[0]) + (fibo[1] * fibo[1]);
// Add remaining terms
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
sum += (fibo[i] * fibo[i]);
}
return sum;
}
// Driver program to test above function
int main()
{
int n = 6;
cout << "Sum of squares of Fibonacci numbers is : "
<< calculateSquareSum(n) << endl;
return 0;
}
Java
// Java Program to find sum of
// squares of Fibonacci numbers
public class Improve {
// Function to calculate sum of
// squares of Fibonacci numbers
static int calculateSquareSum(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[0]) + (fibo[1] * fibo[1]);
// Add remaining terms
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
sum += (fibo[i] * fibo[i]);
}
return sum;
}
// Driver code
public static void main(String args[])
{
int n = 6;
System.out.println("Sum of squares of Fibonacci numbers is : " +
calculateSquareSum(n));
}
// This Code is contributed by ANKITRAI1
}
Python3
# Python3 Program to find sum of
# squares of Fibonacci numbers
# Function to calculate sum of
# squares of Fibonacci numbers
def calculateSquareSum(n):
fibo = [0] * (n + 1);
if (n <= 0):
return 0;
fibo[0] = 0;
fibo[1] = 1;
# Initialize result
sum = ((fibo[0] * fibo[0]) +
(fibo[1] * fibo[1]));
# Add remaining terms
for i in range(2, n + 1):
fibo[i] = (fibo[i - 1] +
fibo[i - 2]);
sum += (fibo[i] * fibo[i]);
return sum;
# Driver Code
n = 6;
print("Sum of squares of Fibonacci numbers is :",
calculateSquareSum(n));
# This Code is contributed by mits
C#
// C# Program to find sum of
// squares of Fibonacci numbers
using System;
public class Improve {
// Function to calculate sum of
// squares of Fibonacci numbers
static int calculateSquareSum(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[0]) + (fibo[1] * fibo[1]);
// Add remaining terms
for (int i = 2; i <= n; i++) {
fibo[i] = fibo[i - 1] + fibo[i - 2];
sum += (fibo[i] * fibo[i]);
}
return sum;
}
// Driver code
public static void Main()
{
int n = 6;
Console.Write("Sum of squares of Fibonacci numbers is : " +
calculateSquareSum(n));
}
}
PHP
C++
// C++ Program to find sum of squares 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];
}
// Function to calculate sum of
// squares of Fibonacci numbers
int calculateSumOfSquares(int n)
{
return fib(n) * fib(n + 1);
}
// Driver Code
int main()
{
int n = 6;
cout << "Sum of Squares of Fibonacci numbers is : "
<< calculateSumOfSquares(n) << endl;
return 0;
}
Java
// Java Program to find sum of squares of
// Fibonacci numbers in O(Log n) time.
class gfg {
static int[] f = new int[1000];
// Create an array for memoization
// Returns n'th Fibonacci number using table f[]
public static 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] > 0) {
return f[n];
}
int k = ((n & 1) > 0) ? (n + 1) / 2 : n / 2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = ((n & 1) > 0) ? (fib(k)
* fib(k) + fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
// Function to calculate sum of
// squares of Fibonacci numbers
public static int calculateSumOfSquares(int n) {
return fib(n) * fib(n + 1);
}
}
// Driver Code
class geek {
public static void main(String[] args) {
gfg g = new gfg();
int n = 6;
System.out.println("Sum of Squares of Fibonacci numbers is : "
+ g.calculateSumOfSquares(n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find sum of squares
# of Fibonacci numbers in O(Log n) time.
MAX = 1000
# Create an array for memoization
f = [0 for i in range(MAX)]
# Returns n'th Fibonacci number using
# table f[]
def fib(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]:
return f[n]
if n & 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:
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]
# Function to calculate sum of
# squares of Fibonacci numbers
def calculateSumOfSquares(n):
return fib(n) * fib(n + 1)
# Driver Code
n = 6
print("Sum of Squares of "
"Fibonacci numbers is :",
calculateSumOfSquares(n))
# This code is contributed by Gaurav Kumar Tailor
C#
// C# Program to find sum of squares of
// Fibonacci numbers in O(Log n) time.
using System;
class gfg
{
int []f = new int [1000];
// Create an array for memoization
// Returns n'th Fibonacci number using table f[]
public 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]>0)
return f[n];
int k = ((n & 1)>0) ? (n + 1) / 2 : n / 2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = ((n & 1)>0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
// Function to calculate sum of
// squares of Fibonacci numbers
public int calculateSumOfSquares(int n)
{
return fib(n) * fib(n + 1);
}
}
// Driver Code
class geek
{
public static int Main()
{
gfg g = new gfg();
int n = 6;
Console.WriteLine( "Sum of Squares of Fibonacci numbers is : {0}", g.calculateSumOfSquares(n));
return 0;
}
}
PHP
输出:
Sum of squares of Fibonacci numbers is : 104
方法2:我们知道,对于第i个斐波那契数,
fi+1 = fi + fi-1 for all i>0
Or, fi = fi+1 - fi-1 for all i>0
Or, fi2 = fifi+1 - fi-1fi
因此,对于任何n> 0,我们都会看到,
f02 + f12 + f22+…….+fn2
= f02 + ( f1f2– f0f1)+(f2f3 – f1f2 ) +………….+ (fnfn+1 – fn-1fn )
= fnfn+1 (Since f0 = 0)
该标识还满足n = 0(对于n = 0,f 0 2 = 0 = f 0 f 1 )。
因此,要找到总和,只需要找到f n和f n + 1即可。在O(log n)时间中找到f n 。请参考本文的方法5或方法6。
下面是上述方法的实现:
C++
// C++ Program to find sum of squares 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];
}
// Function to calculate sum of
// squares of Fibonacci numbers
int calculateSumOfSquares(int n)
{
return fib(n) * fib(n + 1);
}
// Driver Code
int main()
{
int n = 6;
cout << "Sum of Squares of Fibonacci numbers is : "
<< calculateSumOfSquares(n) << endl;
return 0;
}
Java
// Java Program to find sum of squares of
// Fibonacci numbers in O(Log n) time.
class gfg {
static int[] f = new int[1000];
// Create an array for memoization
// Returns n'th Fibonacci number using table f[]
public static 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] > 0) {
return f[n];
}
int k = ((n & 1) > 0) ? (n + 1) / 2 : n / 2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = ((n & 1) > 0) ? (fib(k)
* fib(k) + fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
// Function to calculate sum of
// squares of Fibonacci numbers
public static int calculateSumOfSquares(int n) {
return fib(n) * fib(n + 1);
}
}
// Driver Code
class geek {
public static void main(String[] args) {
gfg g = new gfg();
int n = 6;
System.out.println("Sum of Squares of Fibonacci numbers is : "
+ g.calculateSumOfSquares(n));
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to find sum of squares
# of Fibonacci numbers in O(Log n) time.
MAX = 1000
# Create an array for memoization
f = [0 for i in range(MAX)]
# Returns n'th Fibonacci number using
# table f[]
def fib(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]:
return f[n]
if n & 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:
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]
# Function to calculate sum of
# squares of Fibonacci numbers
def calculateSumOfSquares(n):
return fib(n) * fib(n + 1)
# Driver Code
n = 6
print("Sum of Squares of "
"Fibonacci numbers is :",
calculateSumOfSquares(n))
# This code is contributed by Gaurav Kumar Tailor
C#
// C# Program to find sum of squares of
// Fibonacci numbers in O(Log n) time.
using System;
class gfg
{
int []f = new int [1000];
// Create an array for memoization
// Returns n'th Fibonacci number using table f[]
public 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]>0)
return f[n];
int k = ((n & 1)>0) ? (n + 1) / 2 : n / 2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = ((n & 1)>0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
: (2 * fib(k - 1) + fib(k)) * fib(k);
return f[n];
}
// Function to calculate sum of
// squares of Fibonacci numbers
public int calculateSumOfSquares(int n)
{
return fib(n) * fib(n + 1);
}
}
// Driver Code
class geek
{
public static int Main()
{
gfg g = new gfg();
int n = 6;
Console.WriteLine( "Sum of Squares of Fibonacci numbers is : {0}", g.calculateSumOfSquares(n));
return 0;
}
}
的PHP
输出:
Sum of Squares of Fibonacci numbers is : 104