斐波那契数的平方和的最后一位

#include <iostream>
using namespace std;

int previous_fibonacci_last_digit(unsigned long long m) {
int fib1[m] = { 0 };
fib1[0] = 0;
fib1[1] = 1;
for (unsigned long long i = 2; i <= m; i++) {
    fib1[i] = (fib1[i - 1] + fib1[i - 2]) % 10; // stores the last digit of each fib no. in fib1[i]
    if( (i % 500000) == 0)
        cout << "GOTCHA " << endl;
   }
   cout << "Last digit of " << " fibonacci is for n = " << m << " is " << fib1[m] << endl;

   return fib1[m];
}

int last_digit(unsigned long long n) {
     int lastDigit = (previous_fibonacci_last_digit(n) * (previous_fibonacci_last_digit(n) + previous_fibonacci_last_digit(n - 1))) % 10; // found the last digit of the sum of squares of n fib numbers

     return lastDigit;
  }

为了找到n个fib的平方和的最后一位,我发现该和可以写成F(n){F(n)+ F(n-1)},并且我将其实现为大数值。 当我使用long long int时,我的程序在n = 260548时崩溃了,所以我将其更改为unsigned long long int,现在我的程序在n = 519265时崩溃了。

我尝试通过在previous_Fibonacci_last_digit()函数中添加一个cout来查看循环是否到500000,以进行调试,但我发现当n = 519265时,循环甚至没有运行到500000。我只是存储每个斐波那契数字的最后一位所以我认为其中没有整数溢出。

评论