C#——Fibonacci數列應用

Fibonacci數列求最高次項除以10007的餘數

但就求數列本身並不難,問題在於用最短的時間輪詢到當前最高次項

using System;
/*
    Fibonacci數列的遞推公式爲:Fn = Fn-1+Fn-2,其中F1=F2=1。
    當n比較大時,Fn也非常大,現在我們想知道,Fn除以10007的餘數是多少
*/
namespace demo
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0, b = 1, c = 1, F, n = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < n; i++)
            {
                c = a + b;
                b = a;
                a = c;
            }
            F = c % 10007;
            Console.WriteLine(F);
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章