mike adams - 1 year ago 87
Java Question

# Power calculation without using BigNumber

How do I solve the following?

1. Calculate X^Y without Big Integer or Big Number.

2. Need to print all digits e.g. 2^64 = 18446744073709551616.

I have written code in Java, but I am not getting the exact result for higher digits.

My code:

public class Calculation {

ArrayList<Double> c = new ArrayList<Double>();
double a = 2;
double b = 64;
double[] d = new double[5];

public void powerCalculation()
{
d[0] = java.lang.Math.pow(a, b);
System.out.println(d[0]);
}

public static void main(String[] args)
{
Calculation calculation = new Calculation();
calculation.powerCalculation();
}
}


The mantissa of double isn't long enough to hold the digits of 2^64. The straightforward but hard way would be using something like
ArrayList<Byte> a;

(where a.get(x) means the value of position 10^x) and rewrite the algorithms of add, multiply and power you learned at school.