Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Program to compute powers

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program to compute powers

    Hi there folks.

    I'm trying to create a program that takes 2 numbers from command line arguments and use the second value as a power so for e.g. java Power 10 2 should give 100. But i want to do this without using the Math.pow method, and using a for loop. I have the following so far but i'm not sure what the problem is and it;s only seem to be doinng square numbers (i.e. to the power of 2)
    public class Power
    {
      public static void main(String [] args)
      {
        int mantissa = Integer.parseInt(args[0]);
        int exponent = Integer.parseInt(args[1]);
        int answer;
     
     
        for(int i=0; (mantissa*mantissa) < exponent; i++)
          exponent=exponent + i;
     
        answer = mantissa*mantissa;
        System.out.println("Result is " + answer);
     
     
      }
    }
    Last edited by copeg; October 25th, 2010 at 04:30 PM.


  2. #2
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: Program to compute powers

    Well, all you ever set the value of answer to is mantissa*mantissa so what did you expect?

    Put your code aside. Take a paper and write down the steps to get a power in simple English (or your native language). Check your steps with 3 or 4 sample data sets. When it's all correct (and not before!), convert those steps into Java code.

    db

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program to compute powers

    I know how to get powers, i wrote pseudocode and designed it, but im not sure what the case is for like cubics and powers more than 2.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Program to compute powers

    You know there already is a class called Math that has a method called power that takes two numbers and raises the first number to the power of the second number.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Program to compute powers

    public power(int num, int power)
    {
    int num2;
    if (power == 0 && num!=0)
    {
    num2 = 1;
    }
    else if (power == 1)
    {
    num2 = num;
    }
     
    else if (1 < power)
    {
    num2 = num * power(num, power -1);
    }
     
    else if(0 > power && num!=0)
    {
    num2 = 1/power(num, - power);
    }
    }

    Well...that may not quite work, haven't tried it out yet, but it's on the right track.
    Last edited by javapenguin; October 25th, 2010 at 04:55 PM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Program to compute powers

    You're algorithm doesn't compute x^y.

    Get out a piece of paper and "execute" the code as if you were the computer (pick some sample inputs):

    ex. mantissa = 4, exponent = 3

    4^3 = 64

        for(int i=0; (mantissa*mantissa) < exponent; i++)
          exponent=exponent + i;
    1. initialize i = 0
    2. 4 * 4 = 16
    3. exponent = 3
    4. 16 < 3?
    5. false, exit the for loop

    answer = mantissa*mantissa;
    System.out.println("Result is " + answer);
    6. answer = 4 * 4 = 16
    7. Print out "result is 16"

    Re-visit your pseudo-code and make sure that that is correct.

    As far as I can tell, your code at best will give you mantissa^2, and at worst will keep you stuck indefinately inside the for-loop (well, not really indefinately, integers wrap around at 2^31 so eventually your code will stop and give you mantissa^2)

    edit:

    public power(int num, int power)
    {
     
    if (power == 0 && num!=0)
    {
    num2 = 1;
    }
    else if (power == 1)
    {
    num2 = num;
    }
     
    else if (1 < power)
    {
    num2 = num * power(num, power -1);
    }
     
    else if(0 > power)
    {
    num2 = 1/power(num, - power);
    }
    }
    This isn't quite correct (for various reasons).

    1. This isn't proper Java syntax (no return type, num2 is never declared).
    2. 0^0 = 1. In your code, as far as I can work out it's un-determined
    3. Try running power(2,-1). You'll get a rather interesting result (correct result = 0.5). I actually can't think of a case off the top of my head where this code would give you correct results.
    4. No non-integer inputs for power (or number, but this is easier to fix than non-integer powers), i.e. power(2.5, -1.346)
    5. (minor issue) Assuming this code did actually work, this gives an O(n) solution at best (I didn't perform a thourough analysis, but it can only be worse). The best solution for computing x^y is O(1) (granted, there is a bit of "cheating" by using logarithm tables)
    6. We're here to help people find the answers to their problems, not give the answer to them
    Last edited by helloworld922; October 25th, 2010 at 05:05 PM.

Similar Threads

  1. Compute Wind Chill Assignment
    By JavaCow in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 17th, 2010, 05:35 PM
  2. calculator GUI needs to compute
    By javanovice in forum AWT / Java Swing
    Replies: 3
    Last Post: May 4th, 2010, 02:16 PM
  3. Compute the frequency count and big-oh notation for a certain code segment
    By maykel_trinidad in forum Java Theory & Questions
    Replies: 3
    Last Post: November 13th, 2009, 10:23 AM
  4. Replies: 6
    Last Post: October 23rd, 2009, 03:53 AM