Results 1 to 2 of 2
  1. #1
    williamsb14 is offline Member
    Join Date
    Nov 2012
    Rep Power

    Default solved

    Last edited by williamsb14; 02-15-2013 at 03:30 AM.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Recursion to Iteration exponents Help

    You're doing way too much in your implementation; think of the exponent as a binary number; if bit i is one, the result should be multiplied by base^(i+1); e.g. if the exponent is 5, the result should be b^4*b^1. In Java this means:

    Java Code:
    public double power(double base, int n) {
       double result= 1;
       for (; n > 0; n/= 2, base*= base)
          if ((n%2) != 0) result*= base;
       return result;
    kind regards,

    The only person who got everything done by Friday was Robinson Crusoe.

Similar Threads

  1. Recursion to Iteration, and Vice Versa
    By penguinCoder in forum New To Java
    Replies: 0
    Last Post: 02-13-2013, 11:57 PM
  2. Iteration to Recursion
    By fam2315 in forum New To Java
    Replies: 7
    Last Post: 06-22-2011, 02:58 AM
  3. calculating exponents
    By GPB in forum New To Java
    Replies: 2
    Last Post: 03-21-2010, 12:44 PM
  4. Named Variables and Exponents
    By kathekas in forum New To Java
    Replies: 5
    Last Post: 09-14-2009, 06:58 PM
  5. (Help) Fraction Summation and Exponents
    By SapphireSpark in forum New To Java
    Replies: 19
    Last Post: 10-09-2008, 04:01 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts