• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how to calculate the N to the power of N

 
Mark Guo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Mark Guo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run the code, you can input num = 64 and power = 64 and get the result. The N means num or power.
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me a solution to handle all number type?
 
Henry Wong
author
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21003
77
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At first I thought this might relate to the exam, but now I think it's more general...
 
Mark Guo
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI Bert Bates

I need your general solution, thanks!

 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, I like BigInteger
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic