Reputation: 33
I have been working on a java program in which I need to calculate and store immense values into an array. So far, I get the value by plugging in an entered variable into an exponential function:
Math.pow(7,x);
I've decided to store the value into a BigInteger array, but I do not know how to store these regular manipulated values with the provided constructors BigInteger provides. For the 'x' value, I am actually using quite large numbers which launch the specified value over what longs can store. I seem to come full circle with every solution I think of...simply doing:
bigArray[i] = new BigInteger((long)Math.pow(7,x));
Does not work, since I am dealing with values larger than 100 as x. What can I do?
Upvotes: 3
Views: 569
Reputation: 12561
aioobe's answer already provided, suggesting using BigInteger.valueOf is correct, but I want to provide some additional information.
As he says:
BigInteger bi = BigInteger.valueOf(7).pow(x);
But lets also look at the Java documentation with regard to the "valueOf()" method:
"This "static factory method" is provided in preference to a (long) constructor because it allows for reuse of frequently used BigIntegers."
See: http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#valueOf(long)
Upvotes: 3
Reputation: 15718
Supply string as the param to BigInteger constructor E.g.
BigInteger bigInteger = new BigInteger("1000000000000000000000000000000000000000000000000000000000000000000");
In your case
BigInteger bigInteger = new BigInteger("7").pow(x);
Upvotes: 0