aspose file tools*
The moose likes Java in General and the fly likes how to calculate	the   N to the power of N Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to calculate	the   N to the power of N " Watch "how to calculate	the   N to the power of N " New topic
Author

how to calculate the N to the power of N

Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
Hi Guys

How to calculate the N to the power of N ,

public class ForCycle_04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("begin");
int num = sc.nextInt();
System.out.println("end");
int power = sc.nextInt();
int result = 1;

for (int i = 0; i < power; i++) {
result = result * num;
}
System.out.println(result);
}
}


if N == 64 return 0 , please give me a solution.

Thanks in advance!
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Mark Guo wrote:Hi Guys

How to calculate the N to the power of N ,

public class ForCycle_04 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("begin");
int num = sc.nextInt();
System.out.println("end");
int power = sc.nextInt();
int result = 1;

for (int i = 0; i < power; i++) {
result = result * num;
}
System.out.println(result);
}
}


if N == 64 return 0 , please give me a solution.

Thanks in advance!


The source code that you provided doesn't have an N, so don't know what you mean by "if N == 64 return 0". Second, after clarifying that, please explain what issues you are having with the code that you provided.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
run the code, you can input num = 64 and power = 64 and get the result. The N means num or power.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Mark Guo wrote:run the code, you can input num = 64 and power = 64 and get the result. The N means num or power.



Hint: What is the legal range of an integer ? And what happens when you overflow it ?

Henry
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
Can you give me a solution to handle all number type?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

Mark Guo wrote:Can you give me a solution to handle all number type?


A long has a larger range than an integer. A java.math.BigInteger has a range that is even larger than that. For 64 to the 64th power, the java.math.BigInteger class should be able to handle it.

Henry
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39


BTW, I did a quick and dirty port of the program to use the BigInteger class. And the result of 64 to the 64th power was ....

39402006196394479212279040100143613805079739270465446667948293404245721771497210611414266254884915640806627990306816

Henry
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
At first I thought this might relate to the exam, but now I think it's more general...


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
HI Bert Bates

I need your general solution, thanks!

Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Mark Guo wrote:HI Bert Bates

I need your general solution, thanks!



Henry Wong wrote:BTW, I did a quick and dirty port of the program to use the BigInteger class. And the result of 64 to the 64th power was ....


Henry has given hints for you to come up with a general solution. Is there something that you are stuck with?
Mark Guo
Ranch Hand

Joined: Nov 17, 2010
Posts: 58
Thanks guys, I like BigInteger
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
You realise you can change that linear complexity algorithm to run in logarithmic complexity?
If the index divides exactly by two, you halve the index, and square the result. Remember to get x squared, you write x * x.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to calculate the N to the power of N
 
Similar Threads
Unable to execute the program (no compilation error)
Search Element in array Problem
Trouble changing variable within loop
My Array
Explanation