Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.math.BigInteger;
- /**
- * Created by iamcreasy on 6/15/2016.
- */
- public class Test2 {
- public static void main(String[] args) {
- long currentTime = System.currentTimeMillis();
- System.out.println(fib(1000));
- System.out.println("\nfib function took " + (System.currentTimeMillis() - currentTime) + " miliseconds");
- }
- static BigInteger fib(long nth){
- nth = nth - 1;
- long count = 0;
- BigInteger first = BigInteger.ZERO;
- BigInteger second = BigInteger.ONE;
- BigInteger third = null;
- while(count < nth){
- third = new BigInteger(first.add(second).toString());
- first = new BigInteger(second.toString());
- second = new BigInteger(third.toString());
- count++;
- }
- return third;
- }
- }
- // Output :
- // 4346655768693745643568852767504062580256466051737178040248172908953655541794905189040387984007925516929592259308032
- // 2634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
- //
- // fib function took 41 miliseconds
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement