programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# how to calculate the N to the power of N

Mark Guo
Ranch Hand
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.

Henry Wong
author
Sheriff
Posts: 23280
125
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
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
Posts: 23280
125
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
Can you give me a solution to handle all number type?

Henry Wong
author
Sheriff
Posts: 23280
125
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
Posts: 23280
125

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: 8945
17
At first I thought this might relate to the exam, but now I think it's more general...

Mark Guo
Ranch Hand
Posts: 58
HI Bert Bates

I need your general solution, thanks!

Sridhar Santhanakrishnan
Ranch Hand
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
Posts: 58
Thanks guys, I like BigInteger

Campbell Ritchie
Sheriff
Posts: 55351
157
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.