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

    Default solved

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

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Recursion to Iteration, and Vice Versa
    By penguinCoder in forum New To Java
    Replies: 0
    Last Post: 02-13-2013, 10: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, 11:44 AM
  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
  •