Ask your own question, for FREE!
Computer Science 13 Online
OpenStudy (anonymous):

another java program! i need to "write a for-loop indexed by the int variable k to print out the sum function k^k for k starting from 1 and ending with 10" so it would look like this; sum = 1^1 + 2^2 + 3^3 +..... 10^10 i just need to compute the sum, this is what i got but it doesn't work, could someone tell me why; public class sumten { public static void main(String[] args) { int sum = 0; for(int k = 1; k <= 10; k++) { int expon = (int) java.lang.Math.pow(k, k); sum = sum + expon; } System.out.print(sum); } } another java program! i need to "write a for-loop indexed by the int variable k to print out the sum function k^k for k starting from 1 and ending with 10" so it would look like this; sum = 1^1 + 2^2 + 3^3 +..... 10^10 i just need to compute the sum, this is what i got but it doesn't work, could someone tell me why; public class sumten { public static void main(String[] args) { int sum = 0; for(int k = 1; k <= 10; k++) { int expon = (int) java.lang.Math.pow(k, k); sum = sum + expon; } System.out.print(sum); } } @Compute

OpenStudy (anonymous):

it gives me an answer of -1742412332

OpenStudy (anonymous):

That's what I got, too. The problem is that an int (32 bits) can only hold positive values up to about 2.1 billion. 10^10 is 10 billion, so it overflows the size of an int. What actually happens is that Math.pow() returns a double, which can store numbers bigger than 10 billion. But when you cast that number to an int java gives it the closes value it can use, which is Integer.MAX_VALUE, which is about 2.1 billion. When you add that to the int sum that overflows the size of an int again, but this time it only keeps the lowest 32 bits of the result. That's essentially a random number, for how close it resembles the answer you were hoping for. A way to get around this problem is to use longs instead of ints. But that's only going to get you up to k=15. Once k=16, 16^16 overflows the size of a long (64 bits). The next step to take is to use doubles to store sum and expon (and don't cast the result of Math.pow()). Doubles can store huge values, up to 1.8 * 10^308, but only with about 16 digits of precision (the 16 most significant digits). So at around k=15 you're going to exceed those significant digits and start to lose some precision in your sum. That's probably fine, but I don't know what the requirements of your task are, so I can't say whether or not that's appropriate for you. But I will point out that if it is okay, then k^k by itself is a good estimate for the sum; for k>=15 the error will be less than 2% (and get smaller with larger k's). To get infinite precision and unlimited sizes you can use java.math.BigInteger. It can be somewhat awkward to use because java doesn't have operator overloading, but it will handle anything you throw at it. I just ran this program with k=100 in less than half a second. And the results were exact to all 200 digits. But this is probably all academic. You're probably just beginning to learn programming with java, and this exercise was to teach you about the limits of the primitive data types, and what happens when you exceed (or ignore) them, probably with the intent of then teaching you about 2's-compliment values and arithmetic. It's worth noting that if you had written your own exponentiation function (instead of using Math.pow()) that stored intermediate results as integers (or longs) that you would have gotten a different answer than you got.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!