查找前 N 个偶数索引的斐波那契数列之和的Java程序
对于给定的正整数 N,目的是求 F2 + F4 + F6 +………+ F2n 的值直到 N 数。其中Fi表示第 i 个斐波那契数。
斐波那契数列是下面给定的整数序列中的数字。
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ……
例子:
Input: n = 4
Output: 33
N = 4, So here the fibonacci series will be produced from 0th term till 8th term:
0, 1, 1, 2, 3, 5, 8, 13, 21
Sum of numbers at even indexes = 0 + 1 + 3 + 8 + 21 = 33.
Input: n = 7
Output: 609
0 + 1 + 3 + 8 + 21 + 55 + 144 + 377 = 609.
方法一:
找到所有 Fibonacci 数直到 2n 并仅将偶数指数相加。
Java
// Java Program to find even sum of
// fibonacci Series Till number N
import java.io.*;
class geeksforgeeks {
// Computing the value of first fibonacci series
// and storing the sum of even indexed numbers
static int Fib_Even_Sum(int N)
{
if (N <= 0)
return 0;
int fib[] = new int[2 * N + 1];
fib[0] = 0;
fib[1] = 1;
// Initializing the sum
int s = 0;
// Adding remaining numbers
for (int j = 2; j <= 2 * N; j++) {
fib[j] = fib[j - 1] + fib[j - 2];
// Only considering even indexes
if (j % 2 == 0)
s += fib[j];
}
return s;
}
// The Driver code
public static void main(String[] args)
{
int N = 11;
// Prints the sum of even-indexed numbers
System.out.println(
"Even sum of fibonacci series till number " + N
+ " is: " + +Fib_Even_Sum(N));
}
}
Java
// Java Program to find even indexed
// Fibonacci Sum in O(Log n) time.
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)
{
// 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 = (n % 2 == 1) ? (n + 1) / 2 : n / 2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = (n % 2 == 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 even-indexed Fibonacci Sum
static int calculateEvenSum(int n)
{
return (fib(2 * n + 1) - 1);
}
// Driver program to test above function
public static void main(String[] args)
{
// Get n
int n = 11;
// Find the alternating sum
System.out.println(
"Even indexed Fibonacci Sum upto " + n
+ " terms: " + calculateEvenSum(n));
}
}
Even sum of fibonacci series till number 11 is: 28656
时间复杂度: O(n)
方法二:
It can be clearly seen that the required sum can be obtained thus:
2 ( F2 + F4 + F6 +………+ F2n ) = (F1 + F2 + F3 + F4 +………+ F2n) – (F1 – F2 + F3 – F4 +………+ F2n)
Now the first term can be obtained if we put 2n instead of n in the formula given here.
Thus F1 + F2 + F3 + F4 +………+ F2n = F2n+2 – 1.
The second term can also be found if we put 2n instead of n in the formula given here
Thus, F1 – F2 + F3 – F4 +………- F2n = 1 + (-1)2n+1F2n-1 = 1 – F2n-1.
So, 2 ( F2 + F4 + F6 +………+ F2n)
= F2n+2 – 1 – 1 + F2n-1
= F2n+2 + F2n-1 – 2
= F2n + F2n+1 + F2n+1 – F2n – 2
= 2 ( F2n+1 -1)
Hence, ( F2 + F4 + F6 +………+ F2n) = F2n+1 -1 .
任务是只找到 F 2n+1 -1。
下面是上述方法的实现:
Java
// Java Program to find even indexed
// Fibonacci Sum in O(Log n) time.
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)
{
// 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 = (n % 2 == 1) ? (n + 1) / 2 : n / 2;
// Applying above formula [Note value n&1 is 1
// if n is odd, else 0].
f[n] = (n % 2 == 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 even-indexed Fibonacci Sum
static int calculateEvenSum(int n)
{
return (fib(2 * n + 1) - 1);
}
// Driver program to test above function
public static void main(String[] args)
{
// Get n
int n = 11;
// Find the alternating sum
System.out.println(
"Even indexed Fibonacci Sum upto " + n
+ " terms: " + calculateEvenSum(n));
}
}
Even indexed Fibonacci Sum upto 8 terms: 1596
时间复杂度: O(log n)