Sum of numbers from 1 to N which are in Lucas Sequence

22 Jun, 2022
Given a number N. The task is to find the sum of numbers from 1 to N, which are present in the Lucas Sequence.
The Lucas numbers are in the following integer sequence: 
2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123 ......
Examples:  
Input :  N = 10
Output : 17

Input : N = 5
Output : 10
Approach:  
Loop through all the Lucas numbers which are less than the given value N.
Initialize a sum variable with 0.
Keep on adding these lucas numbers to get the required sum.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find sum of numbers from// 1 to N which are in Lucas Sequence#include <bits/stdc++.h>using namespace std; // Function to return the// required sumint LucasSum(int N){    // Generate lucas number and keep on    // adding them    int sum = 0;    int a = 2, b = 1, c;     sum += a;     while (b <= N) {         sum += b;        int c = a + b;        a = b;        b = c;    }     return sum;} // Driver codeint main(){    int N = 20;    cout << LucasSum(N);    return 0;}
// java program to find sum of numbers from// 1 to N which are in Lucas Sequenceclass GFG{ // Function to return the// required sumstatic int LucasSum(int N){    // Generate lucas number and keep on    // adding them    int sum = 0;    int a = 2, b = 1, c;     sum += a;     while (b <= N) {         sum += b;        c = a + b;        a = b;        b = c;    }     return sum;} // Driver codepublic static void main(String[] args){    int N = 20;    System.out.println(LucasSum(N));     }// This code is contributed by princiraj1992}
# Python3 program to find Sum of# numbers from 1 to N which are# in Lucas Sequence # Function to return the# required Sumdef LucasSum(N):         # Generate lucas number and    # keep on adding them    Sum = 0    a = 2    b = 1    c = 0     Sum += a     while (b <= N):         Sum += b        c = a + b        a = b        b = c     return Sum # Driver codeN = 20print(LucasSum(N)) # This code is contributed# by mohit kumar
// C# program to find sum of numbers from// 1 to N which are in Lucas Sequenceusing System; class GFG{ // Function to return the// required sumstatic int LucasSum(int N){    // Generate lucas number and keep on    // adding them    int sum = 0;    int a = 2, b = 1, c;     sum += a;     while (b <= N)    {         sum += b;        c = a + b;        a = b;        b = c;    }     return sum;} // Driver codepublic static void Main(String[] args){    int N = 20;    Console.WriteLine(LucasSum(N));}} // This code contributed by Rajput-Ji
<?php// PHP program to find sum of numbers from// 1 to N which are in Lucas Sequence // Function to return the required sumfunction LucasSum($N){    // Generate lucas number and    // keep on adding them    $sum = 0;    $a = 2; $b = 1; $c;     $sum += $a;     while ($b <= $N)    {        $sum += $b;        $c = $a + $b;        $a = $b;        $b = $c;    }     return $sum;} // Driver code$N = 20;echo(LucasSum($N));     // This code is contributed// by Code_Mech.?>
<script> // Javascript program to find sum of numbers// from 1 to N which are in Lucas Sequence // Function to return the// required sumfunction LucasSum(N){         // Generate lucas number and keep    // on adding them    var sum = 0;    var a = 2, b = 1, c;     sum += a;     while (b <= N)    {        sum += b;        var c = a + b;        a = b;        b = c;    }    return sum;} // Driver codevar N = 20;document.write(LucasSum(N)); // This code is contributed by rutvik_56 </script>
Output: 
46
Time Complexity: O(n)Auxiliary Space: O(1)  
mohit kumar 29
princiraj1992
Rajput-Ji
Code_Mech
rutvik_56
rishavnitro
series
series-sum
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Merge two sorted arrays
Operators in C / C++
Prime Numbers
Minimum number of jumps to reach end
The Knight's tour problem | Backtracking-1
Algorithm to solve Rubik's Cube
Modulo 10^9+7 (1000000007)
Modulo Operator (%) in C/C++ with Examples
Program for factorial of a number
Program to print prime numbers from 1 to N.