Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
How we find last digit of fibonacci n.. Assume that n is maximum "10^19"..


I know that, in fibonacci last digit is periodical.. in 60 period..

FOR EXAMPLE
fibo[61]
fibo[1]
fibo[121]
fibo[181] ..... all of them finish with number "1".


How we find last digit in "0,5" second ?



I have a code like that.. with "long" and "BigInteger"

C#
import java.util.Scanner;
public class Main {
   public static void main(String[] args) {
       Scanner input = new Scanner (System.in);

       long n;
       while(input.hasNextLong()){
           n = input.nextLong();
           if(n >= 60) n %= 60;
           long fibo_1 = 0;
           long fibo_2 = 1;
           long fibo_3;
           for(long i = 0; i < n; i++){
               fibo_3 = fibo_1 + fibo_2;
               fibo_1 = fibo_2;
               fibo_2 = fibo_3 % 10;
           }
           System.out.println(fibo_2);
       }
   }
}
Posted
Comments
Kenneth Haugland 21-Oct-13 10:15am    
I dont understand your code: Are you trying to calcualte the last digit in the fibonacci numbers? If so there is a lot of values that you dont need to store, but you still need to calcualte the Fibonacci numbers the normal way.
Steven Gerrard 21-Oct-13 16:44pm    
Yes.. I am trying to calcualte the last digit in the fibonacci numbers
nv3 21-Oct-13 11:06am    
And what exactly is your question? If your program works, you have answered your own question, haven't you?

And by the way, what is a useful application for that?
Ed Nutting 21-Oct-13 12:31pm    
I can answer the second bit for him: solving his homework. The "0.5 seconds" requirement just screams "theoretical h/w question"!

Ed :)
Steven Gerrard 21-Oct-13 16:45pm    
can u compile this prg in 0.5 second ?? if u can't, don't talk more.. You don't know me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900