Re: calculating an exponent?

From: Alf P. Steinbach (alfps_at_start.no)
Date: 09/28/04


Date: Tue, 28 Sep 2004 06:29:54 GMT


* Jani Yusef:
> Ok, this one has me stumped.
> Is it possible to calculate an exponent, say, a^n, using only right
> and left shifts along with additions and subtractions?

All arithmetic operations are implemented that way (in software or hardware).

Presumably 'a' and 'n' are non-negative integers.

The only question is then how (in)efficient you want the algorithm to
be. For efficiency it might be a good idea to first consider efficient
multiplication. With multiplication you can then tackle the efficiency of the
exponentiation, e.g. by noting treating odd n and even n differently.

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?


Relevant Pages

  • Re: calculating an exponent?
    ... Alf P. Steinbach wrote: ... >>Is it possible to calculate an exponent, say, a^n, using only right ... >>and left shifts along with additions and subtractions? ... For efficiency it might be a good idea to first consider efficient ...
    (comp.programming)
  • Re: calculating an exponent?
    ... > Is it possible to calculate an exponent, say, a^n, using only right ... > and left shifts along with additions and subtractions? ... private static int multiply{ ...
    (comp.programming)
  • Re: calculating an exponent?
    ... > Alf P. Steinbach wrote: ... For efficiency it might be a good idea to first consider efficient ... as before with 'n', reducing the exponent further, and so on. ...
    (comp.programming)