Skip to content

Instantly share code, notes, and snippets.

@mlushpenko
Created June 15, 2015 13:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlushpenko/463dc843acedbbbfe238 to your computer and use it in GitHub Desktop.
Save mlushpenko/463dc843acedbbbfe238 to your computer and use it in GitHub Desktop.
Fibonacci big numbers
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