mardi 26 septembre 2017

Numbers larger than long long

I am writing a C++ program to generate the series of Fibanachi numbers. This is the 1, 1, 2, 3, 5... series. The 300th number in this series is 359579325206583560961765665172189099052367214309267232255589801. This is well beyond the limits of int or even unsigned long long. How can I continue to represent such large numbers?

Here's my code:

unsigned long long FibLoop(int n)
{
    // Keep track of previous two numbers
    unsigned long long prev[2];
    prev[0] = 1;
    prev[1] = 1;

    // Loop
    for(int i = 2; i <= n; i++)
    {
        prev[i % 2] = prev[0] + prev[1];
        cout << i << "\t" << prev[i % 2] << endl;
    }

    // Return
    return prev[n % 2];
}

Aucun commentaire:

Enregistrer un commentaire