Showing posts with label BigInteger. Show all posts
Showing posts with label BigInteger. Show all posts

Sunday, 4 January 2015

Fibonacci Sequence (Part 3)

I wrote prog66 below to calculate a Fibonacci number supplied as a parameter then I tested it by recalculating the 20th term in the sequence:

andrew@UBUNTU:~/Java$ cat prog66.java
import java.math.BigInteger;
public class prog66
{
public static void main (String args[])
  {
  BigInteger x = BigInteger.valueOf(1);
  BigInteger y = BigInteger.valueOf(1);
  int required_term = Integer.parseInt(args[0]);
  int term = 0;
  boolean finished = false;
  while (!finished)
    {
    term++;
    if (term == required_term)
      {
      System.out.println(x);
      finished = true;
      }
    term++;
    if (term == required_term)
      {
      System.out.println(y);
      finished = true;
      }
    x = x.add(y);
    y = x.add(y);
    }
  }
}
andrew@UBUNTU:~/Java$ javac prog66.java
andrew@UBUNTU:~/Java$ java prog66 20
6765
andrew@UBUNTU:~/Java$
 

Then I ran the UNIX date command to check the time, calculated the 1,000,000th term in the Fibonacci sequence and ran the UNIX date command again. This showed that the program had taken less than 5 minutes to work out the answer:

andrew@UBUNTU:~/Java$ date;java prog66 1000000 > prog66.output;date
Sun Jan  4 19:31:40 GMT 2015
Sun Jan  4 19:36:23 GMT 2015
andrew@UBUNTU:~/Java$


I found a site which had the 1,000,000th Fibonacci number in a file called millionth-fibonacci-number.txt and downloaded it. Then I compared the first few digits in both files and saw that they were the same: 

andrew@UBUNTU:~/Java$ cat millionth-fibonacci-number.txt|more
1953282128707757731632014947596256332443542996591873396953405


andrew@UBUNTU:~/Java$ cat prog66.output|more
1953282128707757731632014947596256332443542996591873396953405

Saturday, 3 January 2015

Fibonacci Sequence (Part 2)

After some research I discovered how to use a BigInteger to calculate larger terms in the Fibonacci sequence as shown in prog65 below:

andrew@UBUNTU:~/Java$ cat prog65.java
import java.math.BigInteger;
public class prog65
{
public static void main (String args[])
  {
  BigInteger x = BigInteger.valueOf(1);
  BigInteger y = BigInteger.valueOf(1);
  int term;
  for (int z=1; z<=50; z++)
    {
    term = 2*z-1;
    System.out.println("Term " + term + " = " + x);
    term++;
    System.out.println("Term " + term + " = " + y);
    x = x.add(y);
    y = x.add(y);
    }
  }
}
andrew@UBUNTU:~/Java$ javac prog65.java
andrew@UBUNTU:~/Java$ java prog65
Term 1 = 1
Term 2 = 1
Term 3 = 2
Term 4 = 3
Term 5 = 5
Term 6 = 8
Term 7 = 13
Term 8 = 21
Term 9 = 34
Term 10 = 55
Term 11 = 89
Term 12 = 144
Term 13 = 233
Term 14 = 377
Term 15 = 610
Term 16 = 987
Term 17 = 1597
Term 18 = 2584
Term 19 = 4181
Term 20 = 6765
Term 21 = 10946
Term 22 = 17711
Term 23 = 28657
Term 24 = 46368
Term 25 = 75025
Term 26 = 121393
Term 27 = 196418
Term 28 = 317811
Term 29 = 514229
Term 30 = 832040
Term 31 = 1346269
Term 32 = 2178309
Term 33 = 3524578
Term 34 = 5702887
Term 35 = 9227465
Term 36 = 14930352
Term 37 = 24157817
Term 38 = 39088169
Term 39 = 63245986
Term 40 = 102334155
Term 41 = 165580141
Term 42 = 267914296
Term 43 = 433494437
Term 44 = 701408733
Term 45 = 1134903170
Term 46 = 1836311903
Term 47 = 2971215073
Term 48 = 4807526976
Term 49 = 7778742049
Term 50 = 12586269025
Term 51 = 20365011074
Term 52 = 32951280099
Term 53 = 53316291173
Term 54 = 86267571272
Term 55 = 139583862445
Term 56 = 225851433717
Term 57 = 365435296162
Term 58 = 591286729879
Term 59 = 956722026041
Term 60 = 1548008755920
Term 61 = 2504730781961
Term 62 = 4052739537881
Term 63 = 6557470319842
Term 64 = 10610209857723
Term 65 = 17167680177565
Term 66 = 27777890035288
Term 67 = 44945570212853
Term 68 = 72723460248141
Term 69 = 117669030460994
Term 70 = 190392490709135
Term 71 = 308061521170129
Term 72 = 498454011879264
Term 73 = 806515533049393
Term 74 = 1304969544928657
Term 75 = 2111485077978050
Term 76 = 3416454622906707
Term 77 = 5527939700884757
Term 78 = 8944394323791464
Term 79 = 14472334024676221
Term 80 = 23416728348467685
Term 81 = 37889062373143906
Term 82 = 61305790721611591
Term 83 = 99194853094755497
Term 84 = 160500643816367088
Term 85 = 259695496911122585
Term 86 = 420196140727489673
Term 87 = 679891637638612258
Term 88 = 1100087778366101931
Term 89 = 1779979416004714189
Term 90 = 2880067194370816120
Term 91 = 4660046610375530309
Term 92 = 7540113804746346429
Term 93 = 12200160415121876738
Term 94 = 19740274219868223167
Term 95 = 31940434634990099905
Term 96 = 51680708854858323072
Term 97 = 83621143489848422977
Term 98 = 135301852344706746049
Term 99 = 218922995834555169026
Term 100 = 354224848179261915075
andrew@UBUNTU:~/Java$