The Wayback Machine - https://web.archive.org/web/20240914123343/https://www.geeksforgeeks.org/find-the-sum-of-first-n-odd-fibonacci-numbers/
Open In App

Find the sum of first N odd Fibonacci numbers

Last Updated : 02 Nov, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Medium
26.82%
22.2K

Given a number, N. Find the sum of first N odd Fibonacci numbers.

Note: The answer can be very large so print the answer modulo 10^9+7.

Examples:  

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

Approach: Odd Fibonacci series is:  

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

Prefix sum of odd Fibonacci series is:  

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

The formula for the sum of first N odd Fibonacci numbers is: 

a(n) = a(n-1) + 4*a(n-2) - 4*a(n-3) + a(n-4) - a(n-5) for n>5  

Below is the implementation of the above approach: 

C++




// CPP program to Find the sum of
// first N odd Fibonacci numbers
#include <bits/stdc++.h>
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    
}


Javascript




<script>
// javascript  program to Find the sum of
// first N odd Fibonacci numbers
 
    var mod = 1000000007;
 
    // Function to calculate sum of first
    // N odd Fibonacci numbers
    function sumOddFibonacci(n) {
        var Sum = Array(n + 1).fill(0);
 
        // base values
        Sum[0] = 0;
        Sum[1] = 1;
        Sum[2] = 2;
        Sum[3] = 5;
        Sum[4] = 10;
        Sum[5] = 23;
 
        for (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
 
     
 
        var n = 6;
        document.write(sumOddFibonacci(n));
 
// This code contributed by umadevi9616
</script>


PHP




<?php
// PHP program to Find the sum of
// first N odd Fibonacci numbers
$mod = 1000000007 ;
 
// Function to calculate sum of
// first N odd Fibonacci numbers
function sumOddFibonacci($n)
{
    global $mod;
    $Sum[$n + 1] = array();
 
    // base values
    $Sum[0] = 0;
    $Sum[1] = 1;
    $Sum[2] = 2;
    $Sum[3] = 5;
    $Sum[4] = 10;
    $Sum[5] = 23;
 
    for ($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
$n = 6;
echo sumOddFibonacci($n);
 
// This code is contributed by jit_t
?>


Output

44


Time Complexity: O(n), Auxiliary Space: O(n)

Efficient approach : Space optimization O(1)

In previous approach we the current value Sum[i] is only depend upon the previous 6 values i.e. Sum[i-1], Sum[i-2], …. Sum[i-5]  So to optimize the space we can keep track of previous and current values by the help of three variables Sum0, Sum1 , …. ,  Sum5 which will reduce the space complexity from O(N) to O(1).  

Implementation Steps:

  • Create variables Sum0, Sum1 , …. ,  Sum5 to keep track of previous values of Sum[].
  • Initialize base case.
  • Create a variable Sum to store current value.
  • Iterate over subproblem using loop and update Sum.
  • After every iteration update Sum0, Sum1 , …. ,  Sum5 for further iterations.
  • At last return Sum.

Implementation:

C++




// CPP program to Find the sum of
// first N odd Fibonacci numbers
#include <bits/stdc++.h>
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
    int Sum0 = 0;
    int Sum1 = 1;
    int Sum2 = 2;
    int Sum3 = 5;
    int Sum4 = 10;
    int Sum5 = 23;
     
    int Sum;
 
    for (int i = 6; i <= n; i++) {
        Sum = ((Sum5 + (4 * Sum4) % mod -
                (4 * Sum3) % mod + mod) % mod +
                (Sum2 - Sum1 + mod) % mod) % mod;
       
            //assigning values for further iteration
            Sum0 = Sum1;
            Sum1 = Sum2;
            Sum2 = Sum3;
            Sum3 = Sum4;
            Sum4 = Sum5;
            Sum5 = Sum;
    }
 
      // return final answer
    return Sum;
}
 
// Driver code
int main()
{
    long long n = 6;
     
      // function call
    cout << sumOddFibonacci(n);
 
    return 0;
}


Java




import java.util.*;
 
public class Main {
   
    // Function to calculate sum of first
    // N odd Fibonacci numbers
    static long sumOddFibonacci(int n)
    {
        int mod = 1000000007;
 
        // base values
        int Sum0 = 0;
        int Sum1 = 1;
        int Sum2 = 2;
        int Sum3 = 5;
        int Sum4 = 10;
        int Sum5 = 23;
 
        int Sum;
 
        for (int i = 6; i <= n; i++) {
            Sum = ((Sum5 + (4 * Sum4) % mod
                    - (4 * Sum3) % mod + mod)
                       % mod
                   + (Sum2 - Sum1 + mod) % mod)
                  % mod;
 
            // assigning values for further iteration
            Sum0 = Sum1;
            Sum1 = Sum2;
            Sum2 = Sum3;
            Sum3 = Sum4;
            Sum4 = Sum5;
            Sum5 = Sum;
        }
 
        // return final answer
        return Sum5;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
        System.out.println(sumOddFibonacci(N));
    }
}


Python




MOD = 1000000007
 
# Function to calculate sum of first
# N odd Fibonacci numbers
def sumOddFibonacci(n):
    # base values
    Sum0 = 0
    Sum1 = 1
    Sum2 = 2
    Sum3 = 5
    Sum4 = 10
    Sum5 = 23
 
    for i in range(6, n+1):
        Sum = ((Sum5 + (4 * Sum4) % MOD -
                (4 * Sum3) % MOD + MOD) % MOD +
                (Sum2 - Sum1 + MOD) % MOD) % MOD
 
        # assigning values for further iteration
        Sum0 = Sum1
        Sum1 = Sum2
        Sum2 = Sum3
        Sum3 = Sum4
        Sum4 = Sum5
        Sum5 = Sum
 
    # return final answer
    return Sum
 
# Driver code
n = 6
 
# function call
print(sumOddFibonacci(n))


C#




using System;
 
class GFG
{
    const int mod = 1000000007;
 
    // Function to calculate the sum of the first N odd Fibonacci numbers
    static long SumOddFibonacci(int n)
    {
        long sum1 = 1;
        long sum2 = 2;
        long sum3 = 5;
        long sum4 = 10;
        long sum5 = 23;
        long sum = 0;
 
        for (int i = 6; i <= n; i++)
        {
            sum = ((sum5 + (4 * sum4) % mod - (4 * sum3) % mod + mod) % mod
                + (sum2 - sum1 + mod) % mod) % mod;
 
            // Shift values for further iterations
            sum1 = sum2;
            sum2 = sum3;
            sum3 = sum4;
            sum4 = sum5;
            sum5 = sum;
        }
 
        // Return the final answer
        return sum;
    }
 
    // Driver code
    static void Main()
    {
        long n = 6;
 
        // Function call
        long result = SumOddFibonacci((int)n);
        Console.WriteLine(result);
    }
}


Javascript




function sumOddFibonacci(n) {
    const mod = 1000000007;
 
    // Initialize base values
    let Sum0 = 0;
    let Sum1 = 1;
    let Sum2 = 2;
    let Sum3 = 5;
    let Sum4 = 10;
    let Sum5 = 23;
 
    let Sum;
 
    for (let i = 6; i <= n; i++) {
        Sum = (((Sum5 + (4 * Sum4) % mod - (4 * Sum3) % mod + mod) % mod + (Sum2 - Sum1 + mod) % mod) % mod);
 
        // Assign values for further iteration
        Sum0 = Sum1;
        Sum1 = Sum2;
        Sum2 = Sum3;
        Sum3 = Sum4;
        Sum4 = Sum5;
        Sum5 = Sum;
    }
 
    // Return the final answer
    return Sum5;
}
 
// Driver Code
const N = 6;
console.log(sumOddFibonacci(N));


Output:

44

Time Complexity: O(n)

Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Count of numbers of length N having prime numbers at odd indices and odd numbers at even indices
Given a number N, the task is to calculate the count of numbers of length N having prime numbers at odd indices and odd numbers at even indices. Example: Input : N = 1Output: 5Explanation : All valid numbers length 1 are 1, 3, 5, 7, 9, here we have only 1 odd index, therefore we have 5 valid numbers. Input: N = 2Output: 20 Explanation: There are 20
5 min read
Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
Prerequisites: Digit DP Given a range [L, R], the task is to count the numbers in this range having the difference between, the sum of digits at even positions and sum of digits at odd positions, as a Fibonacci Number. Note: Consider the position of the least significant digit in the number as an odd position. Examples: Input: L = 1, R = 10 Output:
15 min read
Check if sum of Fibonacci elements in an Array is a Fibonacci number or not
Given an array arr[] containing N elements, the task is to check if the sum of Fibonacci elements of the array is a fibonacci number or not.Examples: Input: arr[] = {2, 3, 7, 11} Output: Yes Explanation: As there are two Fibonacci numbers in the array i.e. 2 and 3. So, the sum of Fibonacci numbers is 2 + 3 = 5 and 5 is also a Fibonacci number. Inpu
7 min read
Kth element in permutation of first N natural numbers having all even numbers placed before odd numbers in increasing order
Given two integers N and K, the task is to find the Kth element in the permutation of first N natural numbers arranged such that all the even numbers appear before the odd numbers in increasing order. Examples : Input: N = 10, K = 3 Output: 6Explanation:The required permutation is {2, 4, 6, 8, 10, 1, 3, 5, 7, 9}.The 3rd number in the permutation is
9 min read
Even numbers at even index and odd numbers at odd index
Given an array of size n containing equal number of odd and even numbers. The problem is to arrange the numbers in such a way that all the even numbers get the even index and odd numbers get the odd index. Required auxiliary space is O(1).Examples : Input : arr[] = {3, 6, 12, 1, 5, 8} Output : 6 3 12 1 8 5 Input : arr[] = {10, 9, 7, 18, 13, 19, 4,
11 min read
Make all the elements of array odd by incrementing odd-indexed elements of odd-length subarrays
Given an array arr[] of size N, the task is to make all the array elements odd by choosing an odd length subarray of arr[] and increment all odd positioned elements by 1 in this subarray. Print the count of such operations required. Examples: Input: arr[] = {2, 3, 4, 3, 5, 3, 2}Output: 2Explanation:In first operation, choose the subarray {2, 3, 4}
9 min read
Check if a M-th fibonacci number divides N-th fibonacci number
Given two numbers M and N, the task is to check if the M-th and N-th Fibonacci numbers perfectly divide each other or not.Examples: Input: M = 3, N = 6 Output: Yes F(3) = 2, F(6) = 8 and F(6) % F(3) = 0 Input: M = 2, N = 9 Output: Yes A naive approach will be to find the N-th and M-th Fibonacci numbers and check if they are perfectly divisible or n
8 min read
Program to find Nth odd Fibonacci Number
Given an integer N. The task is to find the Nth odd Fibonacci number.The odd number fibonacci series is as: 1, 1, 3, 5, 13, 21, 55, 89, 233, 377, 987, 1597.............and so on.Note: In the above series we have omitted even terms from the general fibonacci sequence. Examples: Input: N = 3 Output: 3 Input: N = 4 Output: 5 Approach: On observing car
3 min read
Print first n Fibonacci Numbers using direct formula
Given a number n, the task is to print first n Fibonacci numbers. Prerequisite: Program to print first n Fibonacci Numbers | Set 1 Examples: Input : 7 Output :0 1 1 2 3 5 8 Input : 5 Output :0 1 1 2 3 N-th Fibonacci Number = [(1 + ?5)n - (1 - ?5)n)] / [2n * ?5] Below is the implementation. C/C++ Code // C++ code to print fibonacci // numbers till n
4 min read
Program to print first n Fibonacci Numbers | Set 1
Given an integer N. The task is to find the first N Fibonacci numbers. Examples :  Input: n = 3Output: 0 1 1 Input: n = 7Output: 0 1 1 2 3 5 8 Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.  Program to print first 'n' Fibonacci Numbers using recursion:Below is the idea to solve the problem: Use recursion to fin
7 min read
Find two Fibonacci numbers whose sum can be represented as N
Given an even number N, the task is to find two Fibonacci numbers whose sum can be represented as N. There may be several combinations possible. Print only first such pair. If there is no solution then print -1.Examples: Input: N = 90 Output: 1, 89 Explanation: The first pair with whose sum is equal to 90 is {1, 89}Input: N = 74 Output: -1 Approach
6 min read
Sum of fourth powers of first n odd natural numbers
Write a program to find sum of fourth power of first n odd natural numbers. 14 + 34 + 54 + 74 + 94 + 114 .............+(2n-1)4. Examples: Input : 3 Output : 707 14 +34 +54 = 707 Input : 6 Output : 24310 14 + 34 + 54 + 74 + 94 + 114 Naive Approach: - In this Simple finding the fourth power of the first n odd natural numbers is to iterate a loop from
8 min read
Sum of range in a series of first odd then even natural numbers
The sequence first consists of all the odd numbers starting from 1 to n and then remaining even numbers starting 2 up to n. Let's suppose we have n as 1000. Then the sequence becomes 1 3 5 7....999 2 4 6....1000We are given a range (L, R), we need to find sum of numbers of this sequence in a given range. Note: Here the range is given as (L, R) L an
7 min read
Sum of elements in range L-R where first half and second half is filled with odd and even numbers
Given a number N, create an array such the first half of the array is filled with odd numbers till N, and the second half of the array is filled with even numbers. Also given are L and R indices, the task is to print the sum of elements in the array in the range [L, R]. Examples: Input: N = 12, L = 1, R = 11 Output: 66 The array formed thus is {1,
15+ min read
Sum of square of first n odd numbers
Given a number n, find sum of square of first n odd natural numbers. Examples : Input : 3 Output : 35 12 + 32 + 52 = 35 Input : 8 Output : 680 12 + 32 + 52 + 72 + 92 + 112 + 132 + 152 Recommended PracticePower of Pow | Odd NumbersTry It! A simple solution is to traverse through n odd numbers and find the sum of square. Below is the implementation o
5 min read
Sum of cubes of first n odd natural numbers
Given a number n, find sum of first n odd natural numbers. Input : 2 Output : 28 1^3 + 3^3 = 28 Input : 4 Output : 496 1^3 + 3^3 + 5^3 + 7^3 = 496 A simple solution is to traverse through n odd numbers and find the sum of cubes. C/C++ Code // Simple C++ method to find sum of cubes of // first n odd numbers. #include &lt;iostream&gt; using namespace
5 min read
Sum of first n odd numbers in O(1) Complexity
Given the sequence of odd numbers 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, .... Find the sum of first n odd numbersExamples: Input : n = 2Output : 4Sum of first two odd numbers is 1 + 3 = 4.Input : 5Output : 25Sum of first 5 odd numbers is 1 + 3 + 5 +7 + 9 = 25 A simple solution is to iterate through all odd numbers. [GFGTABS] C++ // A naive CPP
6 min read
Sum of the first N terms of XOR Fibonacci series
Given three positive integers A, B, and N where A and B are the first two terms of the XOR Fibonacci series, the task is to find the sum of the first N terms of XOR Fibonacci series which is defined as follows: F(N) = F(N – 1) ^ F(N – 2) where ^ is the bitwise XOR and F(0) is 1 and F(1) is 2. Examples: Input: A = 0, B = 1, N = 3Output: 2Explanation
15+ min read
Find n terms of Fibonacci type series with given first two terms
Given first two numbers of series, find n terms of series with these two numbers. The given series follows the same concept as Fibonacci series, i.e., n-th term is sum of (n-1)-th and (n-2)-th terms. Examples: Input: first = 5, sec = 8, n = 5 Output: 5, 8, 13, 21, 34 Input: first = 2, sec = 4, n = 5 Output: 2, 4, 6, 10, 16 Approach: The approach is
7 min read
Average of odd numbers till a given odd number
Given an odd number n, find the average of odd numbers from 1 to n.Examples: Input : n = 9 Output : 5 Explanation (1 + 3 + 5 + 7 + 9)/5 = 25/5 = 5 Input : n = 221 Output : 111 Method 1 We can calculate average by adding each odd numbers till n and then dividing sum by count.Below is the implementation of the approach. C/C++ Code // Program to find
8 min read
Count numbers from given range having odd digits at odd places and even digits at even places
Given two integers L and R, the task is to count numbers from the range [L, R] having odd digits at odd positions and even digits at even positions respectively. Examples: Input: L = 3, R = 25Output: 9Explanation: The numbers satisfying the conditions are 3, 5, 7, 9, 10, 12, 14, 16 and 18. Input: L = 128, R = 162Output: 7Explanation: The numbers sa
15+ min read
Check if the n-th term is odd or even in a Fibonacci like sequence
Consider a sequence a0, a1, ..., an, where ai = ai-1 + ai-2. Given a0, a1, and a positive integer n. The task is to find whether an is odd or even.Note that the given sequence is like Fibonacci with the difference that the first two terms can be anything instead of 0 or 1.Examples : Input : a0 = 2, a1 = 4, n =3 Output : Even a2 = 6, a3 = 10 And a3
11 min read
Mean of distinct odd fibonacci nodes in a Linked List
Given a singly linked list containing N nodes, the task is to find the mean of all the distinct nodes from the list whose data value is an odd Fibonacci number. Examples: Input: LL = 5 -&gt; 21 -&gt; 8 -&gt;12-&gt; 3 -&gt; 13 -&gt;144 -&gt; 6Output 10.5Explanation:Fibonacci Nodes present in the Linked List are {5, 21, 8, 3, 13, 144}Odd Fibonacci No
15 min read
Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
Given integers A, B, and N, the task is to find the total number of N-digit numbers whose sum of digits at even positions and odd positions are divisible by A and B respectively. Examples: Input: N = 2, A = 2, B = 5Output: 5Explanation:The only 2-digit numbers {50, 52, 54, 56, 58} with sum of odd-positioned digits equal to 5 and even-positioned dig
15+ min read
Absolute difference between sum of even elements at even indices & odd elements at odd indices in given Array
Given an array arr[] containing N elements, the task is to find the absolute difference between the sum of even elements at even indices &amp; the count of odd elements at odd indices. Consider 1-based indexing Examples: Input: arr[] = {3, 4, 1, 5}Output: 0Explanation: Sum of even elements at even indices: 4 {4}Sum of odd elements at odd indices: 4
5 min read
Maximize the difference of sum of elements at even indices and odd indices by shifting an odd sized subarray to end of given Array.
Given an array arr[] of size N, the task is to maximize the difference of the sum of elements at even indices and elements at odd indices by shifting any subarray of odd length to the end of the array. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6}Output: 3Explanation: Initially sum of elements at even indices = 1 + 3 + 5 = 9Sum of elements at odd ind
13 min read
Count of odd sum Submatrix with odd element count in the Matrix
Given a matrix mat[][] of size N x N, the task is to count the number of submatrices with the following properties: The sum of all elements in the submatrix is odd.The number of elements in the submatrix is odd.Examples: Input: mat[][] = {{1, 2, 3}, {7, 5, 9}, {6, 8, 10}}Output: 8Explanation: As here total 8 submatrix is available in given matrix i
15 min read
Number of ways to represent a number as sum of k fibonacci numbers
Given two numbers N and K. Find the number of ways to represent N as the sum of K Fibonacci numbers. Examples: Input : n = 12, k = 1 Output : 0 Input : n = 13, k = 3 Output : 2 Explanation : 2 + 3 + 8, 3 + 5 + 5. Approach: The Fibonacci series is f(0)=1, f(1)=2 and f(i)=f(i-1)+f(i-2) for i&gt;1. Let's suppose F(x, k, n) be the number of ways to for
8 min read
Sum of Fibonacci Numbers with alternate negatives
Given a positive integer n, the task is to find the value of F1 - F2 + F3 -..........+ (-1)n+1Fn where Fi denotes i-th Fibonacci number.Fibonacci Numbers: The Fibonacci numbers are the numbers in the following integer sequence. 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, .... In mathematical terms, the sequence Fn of Fibonacci numbers is defined
12 min read
Sum of numbers from 1 to N which are in Fibonacci Sequence
Given an integer N, the task is to find the sum of numbers from 1 to N which are in Fibonacci sequence. First few Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, ....Examples: Input: N = 5 Output: 12 1 + 1 + 2 + 3 + 5 = 12Input: N = 10 Output: 20 1 + 1 + 2 + 3 + 5 + 8 = 20 Approach: Loop through all the Fibonacci numbers which are less than N.I
4 min read