lordmat

Problem 25

Jun 15th, 2011
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. /**
  2.  *
  3.  * @author Mat
  4.  */
  5. package problem.pkg25;
  6.  
  7. import java.math.BigInteger;
  8.  
  9. public class Problem25 {
  10.  
  11.     public static void main(String[] args) {
  12.         // Start of the Fibonacci sequence
  13.         BigInteger A = BigInteger.ONE;
  14.         BigInteger B = BigInteger.ONE;
  15.        
  16.         // to make DIGITS contain 10^999 which is a number that has 100 digits
  17.         final BigInteger DIGITS = new BigInteger("10").pow(999);        
  18.        
  19.        
  20.         // for debuging to make sure the number is the correct number of digits
  21.         String s1 = DIGITS.toString();        
  22.         System.out.println("Number of digits in s1: " + s1.length());
  23.        
  24.         // loop starts at 2 as sequence has been set at both ones
  25.         for(int i = 2;;){
  26.             A = A.add(B);
  27.             i++;
  28.             if(A.compareTo(DIGITS) >= 0){
  29.                 System.out.println("i is: " + i );
  30.                 System.out.println("A is: " + A.toString() );
  31.                 break;
  32.             }
  33.            
  34.             B = B.add(A);
  35.             i++;
  36.             if(B.compareTo(DIGITS) >= 0){
  37.                 String s2 = B.toString();
  38.                 System.out.println("i is: " + i + "\n" );
  39.                 System.out.println("B is: " + B.toString());
  40.                 break;
  41.             }            
  42.         }
  43.     }
  44. }
  45. /* output
  46.  * Number of digits in s1: 1000
  47.  * i is: 4782
  48.  * B is: 1070066266382758936764980584457396885083683896632151665013235203375314
  49.  * 5206046940406218891475824897926578046948881775919574843364666725699595129960
  50.  * 3046126274809248218614406943305123477444275027378175308757939166619214925918
  51.  * 6759553966422837148943113074699503439547001985432609723067290192870526447243
  52.  * 7261177158218255484911205250132014786129659313817922355596574520395061375514
  53.  * 6783754322911960212993404826070617539770684706820289548690266618543512452190
  54.  * 0369480641357447470911707619766945691070098024393439617474103736912503231365
  55.  * 5321647736970231677550515951735184605799549194109677783732296657965816465139
  56.  * 0348815425631018422419025984608800011018625555024549393711365165703944762958
  57.  * 4714548523425950428582425306083544435428212611008992863795048006894330309773
  58.  */
Advertisement
Add Comment
Please, Sign In to add comment