📜  找出前N个奇数斐波纳契数的总和

📅  最后修改于: 2021-04-22 07:07:27             🧑  作者: Mango

给定一个数字,N。找到前N个奇数斐波纳契数的总和。

注意:答案可能非常大,因此请以10 ^ 9 + 7为模输出答案。

例子

Input : N = 3
Output : 5
Expanation : 1 + 1 + 3

Input : 6
Output : 44
Explanation : 1 + 1 + 3 + 5 + 13 + 21

方法

奇数斐波那契数列是:

1, 1, 3, 5, 13, 21, 55, 89......

奇斐波那契数列的前缀和为:

1, 2, 5, 10, 23, 44, 99, 188.....

前N个奇数斐波纳契数之和的公式为:

下面是上述方法的实现:

C++
// CPP program to Find the sum of
// first N odd Fibonacci numbers
#include 
using namespace std;
  
#define mod 1000000007
  
// Function to calculate sum of first
// N odd Fibonacci numbers
long long sumOddFibonacci(int n)
{
    long long Sum[n + 1];
  
    // base values
    Sum[0] = 0;
    Sum[1] = 1;
    Sum[2] = 2;
    Sum[3] = 5;
    Sum[4] = 10;
    Sum[5] = 23;
  
    for (int i = 6; i <= n; i++) {
        Sum[i] = ((Sum[i - 1] + (4 * Sum[i - 2]) % mod - 
                  (4 * Sum[i - 3]) % mod + mod) % mod + 
                  (Sum[i - 4] - Sum[i - 5] + mod) % mod) % mod;
    }
  
    return Sum[n];
}
  
// Driver code
int main()
{
    long long n = 6;
  
    cout << sumOddFibonacci(n);
  
    return 0;
}


Java
// Java  program to Find the sum of 
// first N odd Fibonacci numbers 
  
import java.io.*;
  
class GFG {
    static int mod =1000000007; 
  
// Function to calculate sum of first 
// N odd Fibonacci numbers 
static  int sumOddFibonacci(int n) 
{ 
     int Sum[]=new int[n + 1]; 
  
    // base values 
    Sum[0] = 0; 
    Sum[1] = 1; 
    Sum[2] = 2; 
    Sum[3] = 5; 
    Sum[4] = 10; 
    Sum[5] = 23; 
  
    for (int i = 6; i <= n; i++) { 
        Sum[i] = ((Sum[i - 1] + (4 * Sum[i - 2]) % mod - 
                (4 * Sum[i - 3]) % mod + mod) % mod + 
                (Sum[i - 4] - Sum[i - 5] + mod) % mod) % mod; 
    } 
  
    return Sum[n]; 
} 
  
// Driver code 
      
    public static void main (String[] args) {
  
    int n = 6; 
    System.out.println(sumOddFibonacci(n));
    }
//This Code is Contributed by Sachin    
}


Python3
# Python3 program to Find the sum of 
# first N odd Fibonacci numbers 
mod = 1000000007 ;
  
# Function to calculate sum of 
# first N odd Fibonacci numbers 
def sumOddFibonacci(n): 
  
    Sum=[0]*(n + 1); 
  
    # base values 
    Sum[0] = 0; 
    Sum[1] = 1; 
    Sum[2] = 2; 
    Sum[3] = 5; 
    Sum[4] = 10; 
    Sum[5] = 23; 
  
    for i in range(6,n+1): 
        Sum[i] = ((Sum[i - 1] + 
                    (4 * Sum[i - 2]) % mod - 
                    (4 * Sum[i - 3]) % mod + 
                    mod) % mod + (Sum[i - 4] -
                    Sum[i - 5] + mod) % mod) % mod; 
  
    return Sum[n]; 
  
# Driver code 
n = 6; 
print(sumOddFibonacci(n)); 
  
# This code is contributed by mits


C#
// C#  program to Find the sum of 
// first N odd Fibonacci numbers 
  
using System;
  
public class GFG{
  
static int mod =1000000007; 
// Function to calculate sum of first 
// N odd Fibonacci numbers 
static int sumOddFibonacci(int n) 
{ 
    int []Sum=new int[n + 1]; 
  
    // base values 
    Sum[0] = 0; 
    Sum[1] = 1; 
    Sum[2] = 2; 
    Sum[3] = 5; 
    Sum[4] = 10; 
    Sum[5] = 23; 
  
    for (int i = 6; i <= n; i++) { 
        Sum[i] = ((Sum[i - 1] + (4 * Sum[i - 2]) % mod - 
                (4 * Sum[i - 3]) % mod + mod) % mod + 
                (Sum[i - 4] - Sum[i - 5] + mod) % mod) % mod; 
    } 
  
    return Sum[n]; 
} 
  
// Driver code 
      
      
      
    static public void Main (){
        int n = 6; 
    Console.WriteLine(sumOddFibonacci(n)); 
    } 
//This Code is Contributed by Sachin     
}


PHP


输出:
44

参考:https://oeis.org/A174542