Skip to content

Instantly share code, notes, and snippets.

@amadamala
Created March 17, 2011 06:27
Show Gist options
  • Save amadamala/873923 to your computer and use it in GitHub Desktop.
Save amadamala/873923 to your computer and use it in GitHub Desktop.
Read this problem somewhere on the internet. Java solution is here.
import java.util.Arrays;
public class Power2 {
static int resultLen = 5000;
public static void main(String[] args) {
Power2 power = new Power2();
String s = power.power(10000);
System.out.println(s);
}
String power(int n) {
int digits[] = new int[resultLen];
digits[resultLen - 1] = 1;
int t = 0;
for (int i = 0; i < n ; i++ ) {
int p = 0;
for(int j = resultLen -1; j > 0 ; j--) {
t = p + 2 * digits[j];
p = 0;
if(t >= 10) {
p = 1;
t = t - 10;
}
digits[j] = t;
}
}
String s = "";
for(int i = 0; i < digits.length; i++) {
s = s + digits[i];
// remove leading zeros from the string
s = s.replaceFirst("^0+(?!$)", "");
}
return s;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment