Problem 16: What is the sum of the digits of the number 2^1000?
Tried my hand at writing my own UnsignedBigInt… (tailored for this question)
import java.util.LinkedList;
import java.util.List;
class runner
{
public static class UnsignedBigInteger{
private List arr = new LinkedList();
private void _setup(double num){
Byte remainder = 0;
while(num > 0){
remainder = new Byte((byte) (num % 10));
arr.add(remainder);
num = (int)(num/10);
}
arr.add((byte)num);
}
private void _setup(UnsignedBigInteger num){
for(int i=0; i 0 || pull > 0); n++){
div = cur/10;
int rmd = (cur%10);
int diff = arr.get(n) - rmd - pull;
if(diff < 0){
diff += 10;
pull = 1;
}else{
pull = 0;
}
arr.set(n, (byte)diff);
cur = div;
}
if (pull != 0){
arr = new LinkedList(); arr.add((byte) 0);
}
}
private void _power(UnsignedBigInteger arr2){
if(arr2.toString().equals("0")){
arr = new LinkedList(); arr.add((byte) 1);
return;
}
while(arr2.toString().length() > 1 || arr2.toString().charAt(0) - 48 != 1){
arr2.subtract(1);
UnsignedBigInteger original = this.clone();
this.add(original);
}
}
private void _multiply(UnsignedBigInteger arr2){
if(arr2.toString().equals("0")){
arr = new LinkedList(); arr.add((byte) 0);
return;
}
UnsignedBigInteger original = this.clone();
while(arr2.toString().length() > 1 || arr2.toString().charAt(0) - 48 != 1){
arr2.subtract(1);
this.add(original);
}
}
private void _add(UnsignedBigInteger arr2){
byte carry = 0;
for(int n = 0; n 0){
while(carry > 0){
Byte remainder = new Byte((byte) (carry % 10));
arr.add(remainder);
carry = (byte) (carry/10);
}
}
}
public byte get(int i){
return arr.get(i);
}
public void set(int index, byte n){
n = (byte) (n % 10);
if(arr.size() <= index){
arr.add(n);
}else{
arr.set(index, n);
}
}
public int length(){
return arr.size();
}
public UnsignedBigInteger clone(){
return new UnsignedBigInteger(this);
}
public String toString(){
StringBuilder x = new StringBuilder();
boolean hit = false;
for(int i = arr.size()-1; i>-1; i--){
byte y = arr.get(i);
if(!hit && y==0 )continue;
else hit = true;
x.append(y);
}
if(x.length() == 0) x.append(0);
return x.toString();
}
}
public static void main (String[] args) throws java.lang.Exception
{
long time = System.currentTimeMillis();
UnsignedBigInteger num = new UnsignedBigInteger(2);
num.power(1000);
double sum = 0;
for(int i = 0; i< num.length(); i++){
sum += num.get(i);
}
System.out.println(sum);
System.out.println("time: "+(System.currentTimeMillis() - time));
}
}