Created
August 9, 2012 23:18
-
-
Save JossWhittle/3308950 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10700662663827589367649805844573968850836838966321516650132352033753145206046940406218 | |
89147582489792657804694888177591957484336466672569959512996030461262748092482186144069 | |
43305123477444275027378175308757939166619214925918675955396642283714894311307469950343 | |
95470019854326097230672901928705264472437261177158218255484911205250132014786129659313 | |
81792235559657452039506137551467837543229119602129934048260706175397706847068202895486 | |
90266618543512452190036948064135744747091170761976694569107009802439343961747410373691 | |
25032313655321647736970231677550515951735184605799549194109677783732296657965816465139 | |
03488154256310184224190259846088000110186255550245493937113651657039447629584714548523 | |
42595042858242530608354443542821261100899286379504800689433030977321783486454311320576 | |
56598684562886168087186938352973506439862976406600007235629179052070511640776148124918 | |
85830945940566688339109350944456576357666151619317753792891661581327159616877487983821 | |
820492520348473874384736771934512787029218636250627816 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void p25() { | |
System.out.println(fib("1", "1", 2)); | |
} | |
public int fib(String n1, String n2, int num) { | |
if (n1.length() >= 1000) { | |
return num; | |
} | |
return fib(add(n1, n2), n1, num + 1); | |
} | |
public String add(String a, String b) { | |
int len = a.length(); | |
int dif = len - b.length(); | |
if (dif > 0) { | |
for (int i = 0; i < dif; i++) { | |
b = "0" + b; | |
} | |
} else if (dif < 0) { | |
for (int i = 0; i > dif; i--) { | |
a = "0" + a; | |
} | |
} | |
int carry = 0; | |
String result = ""; | |
for (int i = len - 1; i >= 0; i--) { | |
int digit = (int)Character.digit(a.charAt(i), 10) | |
+ (int)Character.digit(b.charAt(i), 10) | |
+ carry; | |
if (digit > 9) { | |
carry = 1; | |
digit -= 10; | |
} else { | |
carry = 0; | |
} | |
result = digit + result; | |
} | |
if (carry == 1) { | |
result = carry + result; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment