Last active
October 12, 2016 01:24
-
-
Save ruslander/c1f8dc64140b34142f0fe5ac5022a8c7 to your computer and use it in GitHub Desktop.
First week algorhitms
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 FibonacciLastDigit { | |
private static int getFibonacciLastDigit(int n) { | |
if (n <= 1) | |
return n; | |
int previous = 0; | |
int current = 1; | |
for (int i = 0; i < n - 1; ++i) { | |
int tmp_previous = previous; | |
previous = current; | |
current = (tmp_previous + current) % 10; | |
} | |
return current; | |
} | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int n = scanner.nextInt(); | |
int v = getFibonacciLastDigit(n); | |
System.out.println(v); | |
} | |
} | |
public class Fibonacci { | |
private static long calc_fib(int n) { | |
if(n <= 1) | |
return n; | |
int[] table = new int [n]; | |
table[0] = 1; | |
table[1] = 1; | |
for (int i = 2; i < table.length; i++) { | |
table[i] = table[i-1] + table[i-2]; | |
} | |
return table[n-1]; | |
} | |
public static void main(String args[]) { | |
Scanner in = new Scanner(System.in); | |
int n = in.nextInt(); | |
System.out.println(calc_fib(n)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment