# BigInteger Problems

Printable View

• 01-12-2014, 04:31 AM
AlexGraal
BigInteger Problems
Hi! This won't compile:

Code:

```import java.math.BigInteger; class Problem48 {   public static void main (String[] args) {     BigInteger sum = new BigInteger(0);     for(int x = 1; x <= 1000; x++) {               sum = sum.add(BigInteger.valueOf(((long)Math.pow(x,x))));       }     }   System.out.println(sum.toString()); // <---this highlights and saying something about needing an <identifier>?   } }```
All I want to do is find the sum of all selfpowers of integer from 1 to 1000: What is wrong with my code?
• 01-12-2014, 05:46 AM
jim829
Re: BigInteger Problems
The error message is because the print statement is outside of the main method. But your problems are far worse than that. You need to read the BigInteger API carefully. Hint: Math.pow() won't work (at least not the way you probably want).

Regards,
Jim
• 01-12-2014, 09:33 AM
JosAH
Re: BigInteger Problems
My female intuition suggests that it should be possible to find x^x given the value (x-1)^(x-1) by induction, but I didn't have my espresso yet ...

kind regards,

Jos (<--- sleepy)
• 01-12-2014, 06:47 PM
AlexGraal
Re: BigInteger Problems
By induction? Can you expand on that? Maybe give a small example?
• 01-12-2014, 09:07 PM
jim829
Re: BigInteger Problems
In the meantime, the BigInteger class can solve your problem. Did you read it?

Regards,
Jim
• 01-12-2014, 09:17 PM
JosAH
Re: BigInteger Problems
Quote:

Originally Posted by AlexGraal
By induction? Can you expand on that? Maybe give a small example?

I think the way to go is by using Newton's binomium; if you know (x-1)^(x-1) (and you know that for x == 1, you can figure out x^x.

kind regards,

Jos
• 01-13-2014, 12:25 AM
AlexGraal
Re: BigInteger Problems
Yes I did - and I specifically looked at this:
Quote:

BigInteger.valueOf(long val): Returns a BigInteger whose value is equal to that of the specified long.
So I see no issue logically to doing
Code:

`BigInteger.add( BigInteger.valueOf((long) Math.pow(x,x)))`
However, I'm still having issues with my declaration and initialization line,
Code:

`BigInteger sum = new BigInteger(0);`
I don't see why I'm getting an error there - Dr Java says that BigInteger class has private access or something like that?
• 01-13-2014, 12:47 AM
jim829
Re: BigInteger Problems
Quote:

Originally Posted by AlexGraal
Yes I did - and I specifically looked at this:

So I see no issue logically to doing
Code:

`BigInteger.add( Math.valueOf((long) Math.pow(x,x)))`
However, I'm still having issues with my declaration and initialization line,
Code:

`BigInteger sum = new BigInteger(0);`

Well, you need to read the documentation again because there is no BigInteger constructor which takes an int as an argument.
And the problem with Math.pow(x,x) is that x is a regular old int and Math.pow takes a double. Now it will do a fine conversion for you and return a double. But that double has limited accuracy (long before x gets to 1000). So you need to re-read the BigInteger documentation carefully. You may notice a method similar to the one you are using.

Regards,
Jim
• 01-13-2014, 01:04 AM
AlexGraal
Re: BigInteger Problems
Ok, I'm going to go through what I see happening and you correct me where I'm wrong.

First, we have Math.pow(). From the API:
Quote:

pow(double a, double b)
Returns the value of the first argument raised to the power of the second argument.
So, I'll need to change the inputs to be doubles. Anyway, my main method:

Code:

```  public static void main (String[] args) {     BigInteger sum = new BigInteger("0");     for(double x = 1; x <= 1000; x++) {               sum = sum.add(BigInteger.valueOf(((long)Math.pow(x,x))));       }   System.out.println(sum.toString());   }```
In line two, I'm creating a BigInteger called sum with a value of 0.
Then, I set a forloop that starts at 1 and goes up to 1000 in increments of 1.

The next line,
Code:

`sum = sum.add(BigInteger.valueOf(((long)Math.pow(x,x))));`
Is setting the value of sum as sum + the value of x^x, which is converted to a long, then converted to a BigInteger.

Am I missing something here? It makes sense to me to do Math.pow, convert that to a long (so that the next part will work), then convert the long to a BigInteger.
• 01-13-2014, 01:33 AM
jim829
Re: BigInteger Problems
You cannot use the Math class with BigInteger unless the arguments show it takes a BigInteger object. BigInteger is a very special class that implements arbitrary precision using internal vectors. It has nothing to do with the typical int or long primitives. For example, a long is 64 bits wide which equates to roughly 20 digits. Compare that to 1000^1000 which is 3001 digits.

What I have been steering you toward is the BigInteger.pow() function. Try these to see the difference.

Code:

```BigInteger b1 = BigInteger.valueOf((long)Math.pow(25,25));  System.out.println(b1);  System.out.println(Math.pow(25,25));  System.out.println(new BigInteger("25").pow(25));```
Regards,
Jim