Results 1 to 10 of 10
Thread: BigInteger Problems
 01122014, 04:31 AM #1Señ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++) { sum = sum.add(BigInteger.valueOf(((long)Math.pow(x,x)))); } } System.out.println(sum.toString()); // <this highlights and saying something about needing an <identifier>? } }
 01122014, 05:46 AM #2Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,805
 Rep Power
 7
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,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01122014, 09:33 AM #3
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,071
 Blog Entries
 7
 Rep Power
 24
Re: BigInteger Problems
My female intuition suggests that it should be possible to find x^x given the value (x1)^(x1) by induction, but I didn't have my espresso yet ...
kind regards,
Jos (< sleepy)The only person who got everything done by Friday was Robinson Crusoe.
 01122014, 06:47 PM #4Señ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?
 01122014, 09:07 PM #5Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,805
 Rep Power
 7
Re: BigInteger Problems
In the meantime, the BigInteger class can solve your problem. Did you read it?
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01122014, 09:17 PM #6
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,071
 Blog Entries
 7
 Rep Power
 24
 01132014, 12:25 AM #7Señ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.
Java Code:BigInteger.add( BigInteger.valueOf((long) Math.pow(x,x)))
Java Code:BigInteger sum = new BigInteger(0);
Last edited by AlexGraal; 01132014 at 12:59 AM.
 01132014, 12:47 AM #8Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,805
 Rep Power
 7
Re: BigInteger Problems
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 reread the BigInteger documentation carefully. You may notice a method similar to the one you are using.
Regards,
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
 01132014, 01:04 AM #9Señ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.
Java 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()); }
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))));
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; 01132014 at 01:09 AM.
 01132014, 01:33 AM #10Senior Member
 Join Date
 Jan 2013
 Location
 Northern Virginia, United States
 Posts
 4,805
 Rep Power
 7
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));
JimThe Java^{TM} Tutorials  SSCCE  Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads

BigInteger
By 7cardcha in forum New To JavaReplies: 9Last Post: 10252011, 06:06 AM 
BigDecimal and BigInteger
By jon80 in forum New To JavaReplies: 8Last Post: 11282010, 12:15 PM 
BigInteger value of
By ccie007 in forum New To JavaReplies: 2Last Post: 09262010, 03:46 PM 
BigInteger
By windows.login in forum New To JavaReplies: 8Last Post: 07132010, 01:10 PM 
very new... Biginteger problems
By willemien in forum New To JavaReplies: 6Last Post: 05022010, 03:17 PM
Bookmarks