• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

how to calculate the N to the power of N

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you give me a solution to handle all number type?
 
Henry Wong
author
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23958
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI Bert Bates

I need your general solution, thanks!

 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, I like BigInteger
 
Marshal
Posts: 80259
428
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic