programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# how to calculate the N to the power of N

Ranch Hand
Posts: 58
• Number of slices to send:
Optional 'thank-you' note:
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.

author
Posts: 23956
142
• Number of slices to send:
Optional 'thank-you' note:

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.

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
• Number of slices to send:
Optional 'thank-you' note:
run the code, you can input num = 64 and power = 64 and get the result. The N means num or power.

Henry Wong
author
Posts: 23956
142
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Can you give me a solution to handle all number type?

Henry Wong
author
Posts: 23956
142
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 23956
142
• Number of slices to send:
Optional 'thank-you' note:

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

author
Posts: 9050
21
• Number of slices to send:
Optional 'thank-you' note:
At first I thought this might relate to the exam, but now I think it's more general...

Mark Guo
Ranch Hand
Posts: 58
• Number of slices to send:
Optional 'thank-you' note:
HI Bert Bates

I need your general solution, thanks!

Ranch Hand
Posts: 317
• Number of slices to send:
Optional 'thank-you' note:

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
• Number of slices to send:
Optional 'thank-you' note:
Thanks guys, I like BigInteger

Marshal
Posts: 79471
379
• Number of slices to send:
Optional 'thank-you' note:
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.