Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.