Skip to content

Instantly share code, notes, and snippets.

@fmamud
Created July 3, 2015 22:47
Show Gist options
  • Save fmamud/2667b5249d73766fa131 to your computer and use it in GitHub Desktop.
Save fmamud/2667b5249d73766fa131 to your computer and use it in GitHub Desktop.
package forkjoin;
import java.math.BigInteger;
public class FibonacciProblem3 {
BigInteger n;
public FibonacciProblem3(int n) {
this.n = BigInteger.valueOf(n);
}
public BigInteger solve() {
return fibonacci(n);
}
private BigInteger fibonacci(BigInteger n) {
BigInteger previous = BigInteger.valueOf(-1), result = BigInteger.ONE;
for (long i = 0; BigInteger.valueOf(i).compareTo(n) <= 0; i++) {
BigInteger sum = result.add(previous);
previous = result;
result = sum;
}
return result;
}
public BigInteger getN() {
return n;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment