We are going to solve a simple task.
The task is here:
http://ki.staszic.waw.pl/task.php?name=znowu
And here goes the transcript: Write a program that calculates n-th Fibbonaci number mod 123456789.
It is as clear as daylight that we are not going to calculate every single Fibbonaci's number as this function is periodical. How to find the period?
We calculate Fibbonaci's numbers like this: Each n-tn number = n-2-th number + n-1-th number.
You cad do it recursively but it will crash for large n. Instead, let's do it by iteration.
for (int i = 2; i < n; i++) {
a = b;
b = c;
c = (a + b);
Brak komentarzy:
Prześlij komentarz