1. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## BigInteger Problems

Hi! This won't compile:

Java 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++) {

}
}
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?

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,433
Rep Power
9

## 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

3. ## 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)

4. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: BigInteger Problems

By induction? Can you expand on that? Maybe give a small example?

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,433
Rep Power
9

## Re: BigInteger Problems

In the meantime, the BigInteger class can solve your problem. Did you read it?

Regards,
Jim

6. ## Re: BigInteger Problems

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

7. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## Re: BigInteger Problems

Yes I did - and I specifically looked at this:
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
Java Code:
`BigInteger.add( BigInteger.valueOf((long) Math.pow(x,x)))`
However, I'm still having issues with my declaration and initialization line,
Java 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?
Last edited by AlexGraal; 01-13-2014 at 12:59 AM.

8. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,433
Rep Power
9

## Re: BigInteger Problems

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

So I see no issue logically to doing
Java Code:
`BigInteger.add( Math.valueOf((long) Math.pow(x,x)))`
However, I'm still having issues with my declaration and initialization line,
Java 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

9. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

## 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:
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:

Java Code:
```  public static void main (String[] args) {
BigInteger sum = new BigInteger("0");
for(double x = 1; x <= 1000; 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,
Java 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.
Last edited by AlexGraal; 01-13-2014 at 01:09 AM.

10. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,433
Rep Power
9

## 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.

Java 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•