Thread: Algorithm Help

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #7
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    For powers:

    a^(m+n) = a^m * a^n

    Suppose you're trying to compute a^20

    a^20 = a^10* a^10
    a^10 = a^5 * a^5
    a^5 = a^2 * a^2 * a
    a^2 = a * a

    If you could make your program do that, you could compute a^2, store it in a variable, and work your way up from there, storing each needed power in a variable. It might not seem more efficient, but in the example above, it changes 19 multiplications into a mere 5 (assuming I counted correctly) Cuts down on overhead incurred from using recursion, too. Just an idea, but it seems like it would help a lot.

    edit:
    This becomes much more useful as the exponents get bigger. It doesn't give any benefit for exponents of 2 and 3. The number of multiplications needed after this method / the number needed to "brute force" it seems to be approximated fairly well by
    Y=2X^(-2/3) for even powers (x represents the power, y represents the return of my method)
    For odd powers it doesn't perform quite as well (because of the extra *a) and it's better represented by
    Y=1.5X^(-.4)

    I feel like a geek now.
    Last edited by confuted; 08-08-2003 at 08:48 AM.
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Implement of a Fast Time Series Evaluation Algorithm
    By BiGreat in forum C Programming
    Replies: 7
    Last Post: 12-04-2007, 02:30 AM
  2. Replies: 4
    Last Post: 12-10-2006, 07:08 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM