Suma de números del 1 al N que están en la Secuencia de Lucas

Dado un número N. La tarea es encontrar la suma de los números del 1 al N, que están presentes en la Secuencia de Lucas .

Los números de Lucas están en la siguiente secuencia entera: 

2, 1, 3, 4, 7, 11, 18, 29, 47, 76, 123 ......

Ejemplos:  

Input :  N = 10
Output : 17

Input : N = 5
Output : 10

Acercarse:  

  • Recorra todos los números de Lucas que son menores que el valor dado N.
  • Inicialice una variable de suma con 0.
  • Sigue sumando estos números de lucas para obtener la suma requerida.

A continuación se muestra la implementación del enfoque anterior:  

C++

// 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 sum
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;
        int c = a + b;
        a = b;
        b = c;
    }
 
    return sum;
}
 
// Driver code
int main()
{
    int N = 20;
    cout << LucasSum(N);
    return 0;
}

Java

// java program to find sum of numbers from
// 1 to N which are in Lucas Sequence
class GFG
{
 
// Function to return the
// required sum
static 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 code
public static void main(String[] args)
{
    int N = 20;
    System.out.println(LucasSum(N));
     
}
// This code is contributed by princiraj1992
}

Python3

# Python3 program to find Sum of
# numbers from 1 to N which are
# in Lucas Sequence
 
# Function to return the
# required Sum
def 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 code
N = 20
print(LucasSum(N))
 
# This code is contributed
# by mohit kumar

C#

// C# program to find sum of numbers from
// 1 to N which are in Lucas Sequence
using System;
 
class GFG
{
 
// Function to return the
// required sum
static 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 code
public static void Main(String[] args)
{
    int N = 20;
    Console.WriteLine(LucasSum(N));
}
}
 
// This code contributed by Rajput-Ji

PHP

<?php
// PHP program to find sum of numbers from
// 1 to N which are in Lucas Sequence
 
// Function to return the required sum
function 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.
?>

Javascript

<script>
 
// Javascript program to find sum of numbers
// from 1 to N which are in Lucas Sequence
 
// Function to return the
// required sum
function 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 code
var N = 20;
document.write(LucasSum(N));
 
// This code is contributed by rutvik_56
 
</script>

Producción: 

46

Complejidad temporal: O(n)
Espacio auxiliar: O(1) 
 

Publicación traducida automáticamente

Artículo escrito por Shashank_Sharma y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *