📜  使用Boost库生成大斐波那契数

📅  最后修改于: 2021-06-01 01:25:37             🧑  作者: Mango

在之前有关斐波那契数列的文章中,我们已经看到了许多生成斐波那契数的方法。在这种方法中,我们将在boost库的帮助下生成斐波那契数。该程序简单地使用“升压/多倍/ cpp_int.hpp”升压其中big_int定义库。也可以使用boost库生成超出long long int范围的斐波那契数。以下是使用Boost库生成斐波那契数的C++实现。

CPP
// C++ implementation to generate large
// number of fibonacci series using the
// boost multiprecision library
#include 
#include 
using big_int = boost::multiprecision::cpp_int;
using namespace std;
 
// function to generate first n
// fibonacci numbers
int fib(unsigned int n)
{
    // seed values
    // 0th and 1st number of the
    // series are 0 and 1
    big_int a = 0;
    big_int b = 1;
    cout << "Term 1 is : " << a << endl;
    cout << "Term 2 is : " << b << endl;
     
    for (unsigned int i = 2; i < n; ++i)
    {
        const big_int c = a + b;
        cout << "Term "<< i + 1 << " is : " << c << endl;
        a = b;
        b = c;
    }
}
 
// Driver code
int main()
{
    unsigned int n = 30;
     
    // function calling
    fib(n);
     
    return 0;
}


Java
// Java implementation to generate large
// number of fibonacci series using the 
// boost multiprecision library
public class GFG
{
   
    // function to generate first n
    // fibonacci numbers
    static void fib(int n)
    {
       
        // seed values
        // 0th and 1st number of the
        // series are 0 and 1
        int a = 0;
        int b = 1;
        System.out.println("Term 1 is : " + a); 
        System.out.println("Term 2 is : " + b);  
           
        for (int i = 2; i < n; ++i)
        {
            int c = a + b;
            System.out.println("Term " + (i + 1) + " is : " + c);
            a = b;
            b = c;
        }
    }
 
  // Driver code
    public static void main(String[] args)
    {
        int n = 30;
       
        // function calling
        fib(n);
    }
}
 
// This code is contributed by divyeshrabadiya07.


Python3
# Python3 implementation to generate large
# number of fibonacci series using the 
# boost multiprecision library
 
# function to generate first n
# fibonacci numbers
def fib( n):
   
    # seed values
    # 0th and 1st number of the
    # series are 0 and 1
    a = 0;
    b = 1;
    print("Term 1 is : " + str(a)); 
    print("Term 2 is : " + str(b));  
    for i in range(2, n):
        c = a + b;
        print("Term " + str(i + 1) + " is : " + str(c));
        a = b;
        b = c;
     
# Driver code
if __name__=='__main__':
    n = 30;
   
    # function calling
    fib(n);
 
# This code is contributed by rutvik_56.


C#
// C# implementation to generate large
// number of fibonacci series using the 
// boost multiprecision library
using System;
class GFG {
     
    // function to generate first n
    // fibonacci numbers
    static void fib(int n)
    {
        
        // seed values
        // 0th and 1st number of the
        // series are 0 and 1
        int a = 0;
        int b = 1;
        Console.WriteLine("Term 1 is : " + a); 
        Console.WriteLine("Term 2 is : " + b);  
        for (int i = 2; i < n; ++i)
        {
            int c = a + b;
            Console.WriteLine("Term " + (i + 1) + " is : " + c);
            a = b;
            b = c;
        }
    }
     
  // Driver code
  static void Main() {
    int n = 30;
        
    // function calling
    fib(n);
  }
}
 
// This code is contributed by divyesh072019


Javascript


输出 :

Term 1 is : 0
Term 2 is : 1
Term 3 is : 1
Term 4 is : 2
Term 5 is : 3
Term 6 is : 5
Term 7 is : 8
Term 8 is : 13
Term 9 is : 21
Term 10 is : 34
Term 11 is : 55
Term 12 is : 89
Term 13 is : 144
Term 14 is : 233
Term 15 is : 377
Term 16 is : 610
Term 17 is : 987
Term 18 is : 1597
Term 19 is : 2584
Term 20 is : 4181
Term 21 is : 6765
Term 22 is : 10946
Term 23 is : 17711
Term 24 is : 28657
Term 25 is : 46368
Term 26 is : 75025
Term 27 is : 121393
Term 28 is : 196418
Term 29 is : 317811
Term 30 is : 514229
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”