0

I am trying to find the last digit of Fibonacci numbers. According to the assignment, I generate the Pisano Period first that is the period with which the sequence of Fibonacci numbers taken modulo n repeats. The cycle of mod 10 equals 60. I wrote the code in C++ implementing the Pisano Period, but for some reason I get 62 values in the end - not sure where those 2 values are coming from.

The sum of the n terms equals: F(n+2) - 1 , implemented in the code below as well:

#include <iostream>
#include <vector>
using namespace std;

vector<uint64_t> fibList; // Fibonacci numbers List
vector<uint64_t> pisanoSequence; // Pisano Sequence list
void generatePisanoSequence(int mod)
{
    fibList.push_back((*(fibList.end()-1) + *(fibList.end()-2)) % mod); // Get the last digits of the next Fibonacci number depending on the modulp. 
    pisanoSequence.push_back(*(fibList.end()-1)); //Put the last digit of the last Fibonacci number in the Pisano Sequence
    if (*(pisanoSequence.end() - 1) == 1 && *(pisanoSequence.end() - 2) == 0) // If we return to having 0 then 1 as inputs to the Pisano sequence that mean we have reached the end of the period of the sequence
    {
        return; // Stop Generating entries
    }
    else
    {
        generatePisanoSequence(mod); // Calculate the next entry.
    }
}
int main()
{
    fibList.push_back(0); // Put 0 to the Fibonacci sequence
    fibList.push_back(1); // Put 1 to the Fibonacci sequence
    pisanoSequence.push_back(0); // Put 0 to the Pisano Sequence
    pisanoSequence.push_back(1); // Put 1 to the Pisano sequence
    generatePisanoSequence(10); // An Examplry Modulos of 1000
    uint64_t n; // Input Fibonacci numbers
    cin >> n;
    n = n % ((pisanoSequence.size()) - 2); //Searching for the right i element // had to delete two of these unwanted elements here
    uint64_t n2 = pisanoSequence[n+2]; //Get the number of F(i)
    n2 = n2 - 1;
    cout << (n2 + 10 ) % 10;
    return 0;
}
Daria
  • 163
  • 1
  • 2
  • 8
  • Doesn't a Fibonacci sequence start with `1` and `1`, not `0` and `1`? – Some programmer dude Dec 09 '19 at 10:09
  • It's correct. Therefore, I pushed back 0 and 1 first. – Daria Dec 09 '19 at 10:10
  • @Someprogrammerdude Both versions yield correct sequence and it shouldn't really matter in most cases (unless there's a specific requirement to use one or another). – Yksisarvinen Dec 09 '19 at 10:16
  • Therefore I am pretty convinced that there is no problem with math, whereas something is wrong with the implementation itself. – Daria Dec 09 '19 at 10:18
  • 4
    You stop, when you have another sequence 0 1 at the end of your vector. Hence you have the 0 and the 1 at the beginning and the end of your vector, but a normal period would contain them only once. – n314159 Dec 09 '19 at 10:20
  • 3
    Your vector has 62 numbers 0 1 1 2 3 5 8 3 1 4 5 9 4 3 7 0 7 7 4 1 5 6 1 7 8 5 3 8 1 9 0 9 9 8 7 5 2 7 9 6 5 1 6 7 3 0 3 3 6 9 5 4 9 3 2 5 7 2 9 1 0 1. When 0 1 repeats you stop. But these 0 1 at the end are not part of the Pisano Period. You have to remove them. So remove them and the period size is 60. – mcabreb Dec 09 '19 at 10:22
  • 2
    There are unneeded things in your code. Your fibList and pisanoSequence have exactly the same content. You only need one. Also, in cout << (n2 + 10) % 10; you don't need to add 10 if you do module 10. – mcabreb Dec 09 '19 at 10:24
  • 1
    @Someprogrammerdude The first element of a Fibonacci sequence is 1, but the zeroth element is 0. Whether a sequence starts with the zeroth or first element is subject to debate. – L. F. Dec 09 '19 at 10:29
  • 1
    As you use the formula for the module sum of n element, you will need to access element n+2, so having a vector of n+2 size (with the extra 0 1) is convenient to avoid undefined behaviour. – mcabreb Dec 09 '19 at 10:36

0 Answers0