Sunday, November 17, 2024
Google search engine
HomeData Modelling & AISum of numbers from 1 to N which are in Fibonacci Sequence

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 = 12
Input: N = 10 
Output: 20 
1 + 1 + 2 + 3 + 5 + 8 = 20 
 

 

Approach: 
 

  • Loop through all the Fibonacci numbers which are less than N.
  • Initialize a sum variable with 0.
  • Keep on adding these Fibonacci numbers to get the required sum.

Below is the implementation of the above approach:
 

C++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the
// required sum
int fibonacciSum(int N)
{
  
    if (N == 0)
        return 0;
  
    // Generate all Fibonacci numbers <= N
    // and calculate the sum
    int sum = 0;
    int a = 1, b = 1, c;
    sum += a;
    while (b <= N) {
  
        sum += b;
        int c = a + b;
        a = b;
        b = c;
    }
    return sum;
}
  
// Driver code
int main()
{
    int N = 20;
    cout << fibonacciSum(N);
  
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
      
// Function to return the
// required sum
static int fibonacciSum(int N)
{
  
    if (N == 0)
        return 0;
  
    // Generate all Fibonacci numbers <= N
    // and calculate the sum
    int sum = 0;
    int a = 1, b = 1, c;
    sum += a;
    while (b <= N)
    {
  
        sum += b;
        c = a + b;
        a = b;
        b = c;
    }
    return sum;
}
  
// Driver code
public static void main(String[] args)
{
    int N = 20;
    System.out.println(fibonacciSum(N));
}
}
  
// This code is contributed by Code_Mech.


Python3




# Python3 implementation of 
# the approach 
  
# Function to return the 
# required sum 
def fibonacciSum(N): 
  
    if N == 0:
        return 0
  
    # Generate all Fibonacci 
    # numbers <= N and calculate
    # the sum 
    Sum = 0
    a, b = 1, 1
    Sum +=
    while b <= N: 
  
        Sum +=
        a, b = b, a +
      
    return Sum
  
# Driver code 
if __name__ == "__main__"
  
    N = 20
    print(fibonacciSum(N)) 
  
# This code is contributed 
# by Rituraj Jain


C#




// C# implementation of the approach
using System;
  
class GFG
{
      
// Function to return the
// required sum
static int fibonacciSum(int N)
{
  
    if (N == 0)
        return 0;
  
    // Generate all Fibonacci numbers <= N
    // and calculate the sum
    int sum = 0;
    int a = 1, b = 1, c;
    sum += a;
    while (b <= N)
    {
  
        sum += b;
        c = a + b;
        a = b;
        b = c;
    }
    return sum;
}
  
// Driver code
public static void Main()
{
    int N = 20;
    Console.WriteLine(fibonacciSum(N));
}
}
  
// This code is contributed by Code_Mech.


PHP




<?php
// PHP implementation of the approach
  
// Function to return the
// required sum
function fibonacciSum($N)
{
    if ($N == 0)
        return 0;
  
    // Generate all Fibonacci numbers <= N
    // and calculate the sum
    $sum = 0;
    $a = 1; $b = 1; $c;
    $sum += $a;
    while ($b <= $N)
    {
        $sum += $b;
        $c = $a + $b;
        $a = $b;
        $b = $c;
    }
    return $sum;
}
  
// Driver code
$N = 20;
  
echo(fibonacciSum($N));
  
// This code is contributed 
// by Code_Mech.
?>


Javascript




<script>
  
// Javascript implementation of the approach
  
// Function to return the
// required sum
function fibonacciSum(N)
{
  
    if (N == 0)
        return 0;
  
    // Generate all Fibonacci numbers <= N
    // and calculate the sum
    var sum = 0;
    var a = 1, b = 1, c;
    sum += a;
    while (b <= N) {
  
        sum += b;
        var c = a + b;
        a = b;
        b = c;
    }
    return sum;
}
  
// Driver code
var N = 20;
document.write( fibonacciSum(N));
  
</script>


Output: 

33

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments