Skip to content

Instantly share code, notes, and snippets.

@JossWhittle
Created August 10, 2012 00:33
Show Gist options
  • Save JossWhittle/3309550 to your computer and use it in GitHub Desktop.
Save JossWhittle/3309550 to your computer and use it in GitHub Desktop.
37107287533902102798797998220837590246510135740250
46376937677490009712648124896970078050417018260538
74324986199524741059474233309513058123726617309629
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
89261670696623633820136378418383684178734361726757
28112879812849979408065481931592621691275889832738
44274228917432520321923589422876796487670272189318
47451445736001306439091167216856844588711603153276
70386486105843025439939619828917593665686757934951
...
... Omitted for clarity
...
40789923115535562561142322423255033685442488917353
44889911501440648020369068063960672322193204149535
41503128880339536053299340368006977710650566631954
81234880673210146739058568557934581403627822703280
82616570773948327592232845941706525094512325230608
22918802058777319719839450180888072429661980811197
77158542502016545090413245809786882778948721859617
72107838435069186155435662884062257473692284509516
20849603980134001723930671666823555245252804609722
53503534226472524250874054075591789781264330331690
public void p13() {
String[] v = {"37107287533902102798797998220837590246510135740250..."};
String result = v[0];
for (int i = 1; i < v.length; i++) {
result = add(result, v[i]);
}
System.out.println(result.substring(0,10));
}
public String add(String a, String b) {
int len = a.length();
int dif = len - b.length();
for (int i = 0; i < dif; i++) {
b = "0" + b;
}
int index = len, 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