📜  用给定的前两项找到斐波那契类型系列的 n 项

📅  最后修改于: 2022-05-13 01:57:58.622000             🧑  作者: Mango

用给定的前两项找到斐波那契类型系列的 n 项

给定数列的前两个数,找出这两个数的n个数列。给定的数列遵循与斐波那契数列相同的概念,即第 n 项是第 (n-1) 项和第 (n-2) 项的总和。
例子:

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

方法:
该方法类似于寻找斐波那契数列,其中最后两项的总和形成下一项。找到前两个给定数字的总和,第二个数字现在将作为第一个数字来找到下一项,产生的总和将作为第二个数字来找到下一项。这两个新形成的第一和第二学期的总和将形成下一个所需的学期。
下面是实施方案:

C++
// C++ program to find n terms of
// series from given two numbers
#include 
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


Javascript


输出:
2 4 6 10 16

类似地,要找到第 N 个数,按上述方式生成 N 个数列并打印第 N 个数。