Created
June 15, 2015 13:18
-
-
Save mlushpenko/463dc843acedbbbfe238 to your computer and use it in GitHub Desktop.
Fibonacci big numbers
This file contains 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 int solution(int N) { | |
// write your code in Java SE 8 | |
BigInteger fib = fibonacci(N); | |
System.out.println(fib); | |
int remainder = fib.mod(new BigInteger("1000000")).intValue() ; | |
return remainder; | |
} | |
public BigInteger fibonacci(int i){ | |
if (i < 2) | |
return new BigInteger(String.valueOf(i)); | |
else { | |
int k = 3; | |
BigInteger prev = new BigInteger("1"); | |
BigInteger cur = new BigInteger("2"); | |
while (k < i) { | |
BigInteger fib = prev.add(cur); | |
prev = cur; | |
cur = fib; | |
k++; | |
} | |
return cur; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment