Created
August 10, 2016 16:11
-
-
Save nichtemna/f53386a8480829cb5913a9e8e84792f3 to your computer and use it in GitHub Desktop.
Last Digit of a Large Fibonacci Number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| */ | |
| public class FibLast { | |
| public static void main(String[] args) { | |
| Scanner scaner = new Scanner(System.in); | |
| int n = scaner.nextInt(); | |
| System.out.println("Last digit for " + n + " = " + getFibonacci(n)); | |
| } | |
| private static int getFibonacci(int n) { | |
| int[] fib = new int[n+1]; | |
| for (int i = 0; i < fib.length; i++) { | |
| if (i <= 1) { | |
| fib[i] = i; | |
| } else { | |
| fib[i] = (fib[i-1]+ fib[i-2]) % 10; | |
| } | |
| } | |
| for (int i : fib) { | |
| System.out.println(i); | |
| } | |
| return fib[n]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment