Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
IntelMole
Grand Gerbil Poohbah
Topic Author
Posts: 3506
Joined: Sat Dec 29, 2001 7:00 pm
Location: The nearest pub
Contact:

A bit of fun - Quick exponentiation...

Tue Oct 18, 2005 8:06 am

Because of the way it was written advertising a £5.19 pot of paint, I was trying to do 5^19 today in my head. I summarised that the easiest wayI could find to do this on paper would be as follows:

5^19 = 5^(20-1) = (5^20)/5

5^20 = (5^4)^5

Therefore 5^19 = [(5^4)^5]/5


Done using the pow function in C, that would only require 9 multiplications and a divide by 5, as opposed to 19 multiplications to get the figure originally. Obviously, this isn't a great speed up, but with larger exponents it could be. And at the end of the day, this was just a bit of fun to me.

If a number is prime, you just raise to the next non-prime exponent (add one :D) and remember to divide by five at the end...

Are there any quick ways to find two numbers that will come reasonably close (+ or - a few) to the desired exponent?

Maybe some kind of LCM method would be good, but then you're adding divisions and arrays and things...
-Mole
Living proof of John Gabriel's theorem
 
thegleek
Darth Gerbil
Posts: 7460
Joined: Tue Jun 10, 2003 11:06 am
Location: Detroit, MI
Contact:

Tue Oct 18, 2005 3:32 pm

{first 10-digit prime found in consecutive digits of e}.com

-what website am i?

(i know answer is on the internet, so show me the work how you got the answer!)
––•–√\/––√\/––•–– nostalgia is an emotion for people with no future ––•–√\/––√\/––•–-
 
UberGerbil
Grand Admiral Gerbil
Posts: 10368
Joined: Thu Jun 19, 2003 3:11 pm

Re: A bit of fun - Quick exponentiation...

Tue Oct 18, 2005 4:34 pm

IntelMole wrote:
Done using the pow function in C, that would only require 9 multiplications and a divide by 5, as opposed to 19 multiplications to get the figure originally.
Except divides are relatively expensive (if possible you multiply by 1/x instead), and there are better ways to go than 19 multiplications anyway. Modern fp units have lookups to make logs and exps cheap; even the x87 has several instructions for that.
 
SecretSquirrel
Minister of Gerbil Affairs
Posts: 2726
Joined: Tue Jan 01, 2002 7:00 pm
Location: North DFW suburb...
Contact:

Re: A bit of fun - Quick exponentiation...

Tue Oct 18, 2005 4:56 pm

IntelMole wrote:
Done using the pow function in C, that would only require 9 multiplications and a divide by 5, as opposed to 19 multiplications to get the figure originally. Obviously, this isn't a great speed up, but with larger exponents it could be. And at the end of the day, this was just a bit of fun to me.



You can also do it as 19 shifts and 19 adds, significantly faster than 19 multiplies. Probably not as quick as dumping it to an FP unit, but when talking about stuff like this, one has to realize there are a lot a processors floating around that don't have FP units.

--SS
 
AMM
Gerbil Jedi
Posts: 1996
Joined: Tue Jun 10, 2003 9:12 am
Location: London
Contact:

Re: A bit of fun - Quick exponentiation...

Sat Oct 22, 2005 10:04 am

IntelMole wrote:
If a number is prime, you just raise to the next non-prime exponent (add one :D) and remember to divide by five at the end...


It would take longer to work out if any given number is prime than to actually just do the multiplications probally.
 
Yahoolian
Grand Gerbil Poohbah
Posts: 3577
Joined: Sun Feb 16, 2003 3:43 pm
Location: MD
Contact:

Sat Oct 22, 2005 12:07 pm

So I assume you never timed it to see if it was actually faster. Not exactly the best way to do speed optimizations.
 
morphine
TR Staff
Posts: 11600
Joined: Fri Dec 27, 2002 8:51 pm
Location: Portugal (that's next to Spain)

Sat Oct 22, 2005 12:25 pm

SecretSquirrel nailed it precisely. If you know the number you're multiplying by beforehand, you can take the multiplication operations and separate them into left-shifts and adds. Likewise, you can take division operations and separate them into right-shifts and divisions.
 
IntelMole
Grand Gerbil Poohbah
Topic Author
Posts: 3506
Joined: Sat Dec 29, 2001 7:00 pm
Location: The nearest pub
Contact:

Re: A bit of fun - Quick exponentiation...

Sat Oct 22, 2005 7:14 pm

AMM wrote:
IntelMole wrote:
If a number is prime, you just raise to the next non-prime exponent (add one :D) and remember to divide by five at the end...


It would take longer to work out if any given number is prime than to actually just do the multiplications probally.


I realised that too on the way home :-D

Answer is to forget about prime numbers, and just mod 2 the sucker.
-Mole
Living proof of John Gabriel's theorem

Who is online

Users browsing this forum: No registered users and 17 guests
GZIP: On