Results 1 to 2 of 2
- 02-14-2013, 08:54 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 4
- Rep Power
- 0
- 02-14-2013, 09:49 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,605
- Blog Entries
- 7
- Rep Power
- 17
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:
kind regards,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; }
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Recursion to Iteration, and Vice Versa
By penguinCoder in forum New To JavaReplies: 0Last Post: 02-13-2013, 10:57 PM -
Iteration to Recursion
By fam2315 in forum New To JavaReplies: 7Last Post: 06-22-2011, 02:58 AM -
calculating exponents
By GPB in forum New To JavaReplies: 2Last Post: 03-21-2010, 11:44 AM -
Named Variables and Exponents
By kathekas in forum New To JavaReplies: 5Last Post: 09-14-2009, 06:58 PM -
(Help) Fraction Summation and Exponents
By SapphireSpark in forum New To JavaReplies: 19Last Post: 10-09-2008, 04:01 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks