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.
- 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; } |
chevron_right
filter_none
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. |
chevron_right
filter_none
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 += a while b <= N: Sum += b a, b = b, a + b return Sum # Driver code if __name__ == "__main__": N = 20 print(fibonacciSum(N)) # This code is contributed # by Rituraj Jain |
chevron_right
filter_none
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. |
chevron_right
filter_none
PHP
Output:
33
Recommended Posts:
- Look-and-Say Sequence
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Find if given number is sum of first n natural numbers
- Program for Fibonacci numbers in PL/SQL
- Find product of prime numbers between 1 to n
- Largest of two distinct numbers without using any conditional statements or operators
- Algorithm to generate positive rational numbers
- Lexicographical smallest alternate Array
- Applications of Computer Graphics
- Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
- Flat Panel Display
- Computer Animation
- Relationship between Data Mining and Machine Learning
- Management Information System (MIS) Models
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



