Encuentre n términos de la serie tipo Fibonacci con los dos primeros términos dados

Dados los primeros dos números de la serie, encuentre n términos de la serie con estos dos números. La serie dada sigue el mismo concepto que la serie de Fibonacci , es decir, el término n-ésimo es la suma de los términos (n-1)-ésimo y (n-2)-ésimo.
Ejemplos: 
 

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

Enfoque: 
El enfoque es similar a encontrar series de Fibonacci donde la suma de los dos últimos términos forman el siguiente término. Encuentre la suma de los dos primeros números dados, el segundo número ahora servirá como primer número para encontrar el siguiente término y la suma producida servirá como segundo número para encontrar el siguiente término. La suma de estos dos términos recién formados, primero y segundo, formará el siguiente término requerido.
A continuación se muestra el programa de implementación:
 

C++

// C++ program to find n terms of
// series from given two numbers
#include <bits/stdc++.h>
using namespace std;
 
// Function to find n terms
// of series
void findSeries(int num, int first, int sec)
{
 
    cout << first << " " << sec << " ";
    int counter = 0, sum;
 
    // find next (num - 2) terms of series
    // as first two terms are already given
    while (counter < num - 2) {
        sum = first + sec;
        cout << sum << " ";
        first = sec;
        sec = sum;
        counter++;
    }
}
 
// Drivers code
int main()
{
 
    int n = 5, first = 2, sec = 4;
    findSeries(n, first, sec);
 
    return 0;
}

Java

// Java program to find n terms of
// series from given two numbers
import java.io.*;
 
class GFG {
 
    // Function to find n terms
    // of series
    static void findSeries(int num,
                 int first, int sec)
    {
     
        System.out.print(first + " "
                       + sec + " ");
        int counter = 0, sum;
     
        // find next (num - 2) terms
        // of series as first two
        // terms are already given
        while (counter < num - 2)
        {
            sum = first + sec;
            System.out.print( sum + " ");
            first = sec;
            sec = sum;
            counter++;
        }
    }
     
    // Drivers code
    public static void main (String[] args)
    {
        int n = 5, first = 2, sec = 4;
         
        findSeries(n, first, sec);
    }
}
 
// This code is contributed by vt_m.

Python3

# Python3 program to find n terms of
# series from given two numbers
 
# Function to find n terms
# of series
def findSeries(num, first, sec) :
    print ("{} {} ".format(first, sec),
                               end="")
    counter = 0
 
    # find next (num - 2) terms of
    # series as first two terms are
    # already given
    while (counter < num - 2):
        sum = first + sec
        print ("{} ".format(sum), end="")
        first = sec
        sec = sum
        counter = counter + 1
 
# Drivers code
n = 5
first = 2
sec = 4
findSeries(n, first, sec)
 
# This code is contributed by
# Manish Shaw (manishshaw1)

C#

// C# program to find n terms of
// series from given two numbers
using System;
 
class GFG {
 
    // Function to find n terms
    // of series
    static void findSeries(int num,
                int first, int sec)
    {
     
        Console.Write(first + " "
                    + sec + " ");
        int counter = 0, sum;
     
        // find next (num - 2) terms
        // of series as first two
        // terms are already given
        while (counter < num - 2)
        {
            sum = first + sec;
            Console.Write( sum + " ");
            first = sec;
            sec = sum;
            counter++;
        }
    }
     
    // Drivers code
    public static void Main ()
    {
        int n = 5, first = 2, sec = 4;
         
        findSeries(n, first, sec);
    }
}
 
// This code is contributed by anuj_67.

PHP

<?php
// PHP program to find n terms of
// series from given two numbers
 
// Function to find n terms
// of series
function findSeries( $num, $first, $sec)
{
 
    echo $first , " " , $sec , " ";
    $counter = 0; $sum;
 
    // find next (num - 2) terms of series
    // as first two terms are already given
    while ($counter < $num - 2)
    {
        $sum = $first + $sec;
        echo $sum , " ";
        $first = $sec;
        $sec = $sum;
        $counter++;
    }
}
 
    // Driver Code
    $n = 5;
    $first = 2;
    $sec = 4;
    findSeries($n, $first, $sec);
 
// This code is contributed by anuj_67.
?>

Javascript

<script>
// javascript program to find n terms of
// series from given two numbers
 
// Function to find n terms
// of series
function findSeries( num,  first,  sec)
{
    document.write(first + " " + sec + " ");
    let counter = 0, sum;
 
    // find next (num - 2) terms of series
    // as first two terms are already given
    while (counter < num - 2)
    {
        sum = first + sec;
        document.write(sum + " ");
        first = sec;
        sec = sum;
        counter++;
    }
}
 
// Drivers code
    let n = 5, first = 2, sec = 4;
    findSeries(n, first, sec);
     
// This code is contributed by Rajput-Ji
 
</script>
Producción: 

2 4 6 10 16

 

Complejidad de tiempo: O(n), donde N representa el entero dado.
Espacio auxiliar: O(1), no se requiere espacio adicional, por lo que es una constante.

De manera similar, para encontrar el número N, genere N términos de la serie de la manera anterior e imprima el número N.
 

Publicación traducida automáticamente

Artículo escrito por Bhashkar_P 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 *