This week's giveaway is in the Spring forum. We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line! See this thread for details.

I just started using Java, I completed my homework assignment in VB, but now I want to do it in java. Problem: What is the largest value of n for which 2 to the nth power has fewer than 100 decimal digits. Also, fewer than 1000 decimal digits? I don't know how to tell the length of a string, or a double, or what ever type I should be using for this problem? I also don't know how to raise a number to a power? I guess just using a loop and multplying it again and again? It seems there should be a better way, but I can't find it. The more help the better, any code examples are greatly appriciated. I seem to learn best from them.

Check out the java.lang.Math class. It has a method for raising powers (pow) plus lots of other useful stuff. I suspect your homework is a programming exercise rather than a Math problem, but I think there is a simple way to calculate this. It's a long time since I was at high school, but to determine "n", can't you just calc the log to base 2 of the target and take the integer part? e.g. log to base 2 of 100 is 6.629 so n = 6. In Java, a method to do this would be:

This runs into trouble for very large numbers due to the size of a double. e.g. 1 followed by 1000 digits is too large. If the target is always a power of 10, then you could use:

Travis Gn
Greenhorn

Joined: Sep 11, 2003
Posts: 19

posted

0

I took your advise about the math. I think that saved me lines of code and hours of headaches. Thank you. Since you were able to help with the math on that last one, I am working on another. x^x = 10^100 and x^x = 10^1000 I read over some stuff: exp(ln(x)*ln(x) = 100*ln(10) ??? another thing I read talked about a "W" function? It lost me though. I am very thankful for any help you provide. Travis

Gary Mann
Ranch Hand

Joined: Jun 05, 2003
Posts: 37

posted

0

Sorry - none of this means anything to me. There is info on the net regarding the W function but that is advanced Mathematics. If that's what you're doing, a Java forum probably isn't the place to find answers. It may be time to check in with your teacher! Good Luck.