version: desktop | mobile

Dave's Free Press

the Slightly Less Bad And Wrong Blog Edition

 

Recent posts

(subscribe)

Recently commented posts

(subscribe)

Less important journals

Search

geeky politics silly review culture rant music religion meta weird books drinking perl london media transport cooking language hacking photography olympics whisky maths web spam film phone holidays sport bastards iphone bryar rsnapshot palm telecoms bbc cars travel clothes lolcats amazon etiquette yapc deafness home radio environment latin privacy linux curry security unix work go anglo-saxon review-of-reviews gps
Wed, 27 Aug 2008

Optimising n! - revisited

For the background, see this post.

Late one night I thought that you might be able to simplify n-factorial thus:

Take the product of p(i)int(n/p(i)) for i=1 to i=Φ(n). Damnit, I wish there was a good and easy way of writing mathemagics on t'interwebnet.

Where:

  • n is the number whose factorial we wish to calculate;
  • p(i) is the ith prime number;
  • Φ(n) is the number of primes less than or equal to n

Now, without using a lookup table, p() and Φ() are hard to calculate, but at least you'd avoid a lot of the problems that come from using the stupendously big numbers that come as intermediate results in calculating factorials.

Unfortunately, that formula is wrong anyway. It's a restatement of this:

100! = 250 * 333 * 520 * 714 * 119 * ...

which came from noticing that 100! is the product of 50 numbers which have 2 as a prime factor, 33 numbers which have 3 as a prime factor, 20 numbers that have 5 as a prime factor, and so on. Unfortunately, it doesn't take account of numbers like 4 and 18 which have a repeated prime factor - 4, for example, is 2 * 2 and 18 is 2 * 3 * 3. Bother.

Posted at 17:13:10 by David Cantrell
keywords: geeky | maths
Permalink | 0 Comments

Archive

2009

Jan Feb Mar Apr
May Jun Jul Aug
Sep Oct Nov Dec
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan

2008

Jan Feb Mar Apr
May Jun Jul Aug
Sep Oct Nov Dec
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan

2007

Jan Feb Mar Apr
May Jun Jul Aug
Sep Oct Nov Dec
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan

2006

Jan Feb Mar Apr
May Jun Jul Aug
Sep Oct Nov Dec
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan

2005

Jan Feb Mar Apr
May Jun Jul Aug
Sep Oct Nov Dec
Dec
Nov
Oct
Sep
Aug
Jul
Jun
May
Apr
Mar
Feb
Jan