## Factoring Factorials

### January 24, 2014

The trick is to build up the solution from the primes less than *n* instead of breaking down the solution starting from *n*!. Our solution is due to Will Ness. Consider 33!:

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 5 5 5 5 5 5 5 7 7 7 7 11 11 11 13 13 17 19 23 29 31

The pattern is obvious: For primes less than the square root of *n*, compute the number of times each prime and each prime power appears. For the remaining primes less than half of *n*, compute the number of times each prime appears (but not each prime power). Then each prime that still remains appears once. Like this:

33! = 2^( 33 div 2 + 33 div 4 + 33 div 8 + 33 div 16 + 33 div 32) * 3^( 33 div 3 + 33 div 9 + 33 div 27) * 5^( 33 div 5 + 33 div 25) * ---- 7^( 33 div 7) * 11^( 33 div 11) * 13^( 33 div 13) * ---- 17 * 19 * 23 * 29 * 31

It’s easy to reduce that calculation to Scheme:

`(define (fact-fact n) ; prime factors of n factorial`

(let loop ((ps (primes n)) (fs (list)))

(cond ((null? ps) (reverse fs))

((< (* (car ps) (car ps)) n)

(let ((p (car ps)))

(let ((k (let loop ((q p) (k 0))

(if (< n q) k

(loop (* q p) (+ k (quotient n q)))))))

(loop (cdr ps) (cons (cons (car ps) k) fs)))))

((< (+ (car ps) (car ps)) n)

(loop (cdr ps) (cons (cons (car ps) (quotient n (car ps))) fs)))

(else (loop (cdr ps) (cons (cons (car ps) 1) fs))))))

For example:

`> (fact-fact 33)`

((2 . 31) (3 . 15) (5 . 7) (7 . 4) (11 . 3) (13 . 2) (17 . 1)

(19 . 1) (23 . 1) (29 . 1) (31 . 1))

You can run the program at http://programmingpraxis.codepad.org/nFdoreYl, where there is also a demonstration that the calculation of the factors of 33! is correct.

Pages: 1 2

#include

#include

// reducing numbers from biggest to 2

// 16 -> 2*8 … 8 -> 2*4 … 4 -> 2*2 …

void main(int argc, char **argv) {

int n = atoi(argv[1]);

int *p = (int *) malloc(sizeof(int) * (n + 1));

int i, j, d;

for(i = 0; i 1; i–)

if(p[i]) {

for(j = i + i, d = 2; j <= n; j += i, d++) {

if(p[j]) {

p[i] += p[j];

p[d] += p[j];

p[j] = 0;

}

}

}

printf("1");

for(i = 2; i <= n; i++)

if(p[i])

printf(" * %i^%i", i, p[i]);

printf("\n");

}

Recursivelly adding factorizations

import Data.List

import Data.Monoid

import Data.Numbers.Primes

facfac 1 = Factors [(1, 1)]

facfac n = factorization n `mappend` facfac (n – 1)

— Factorizations Monoid

data Factorization a = Factors [(a, a)] deriving Show

factorization = Factors . map (\ps -> (head ps, length ps)) . group . primeFactors

instance Integral a => Monoid (Factorization a) where

mempty = Factors []

mappend (Factors []) fs = fs

mappend fs (Factors []) = fs

mappend xfs@(Factors (xf@(x, a):xs)) yfs@(Factors (yf@(y, b):ys)) =

case x `compare` y of

EQ -> Factors ((x, a + b):ms)

LT -> Factors (xf:ns)

GT -> Factors (yf:os)

where Factors ms = mappend (Factors xs) (Factors ys)

Factors ns = mappend (Factors xs) yfs

Factors os = mappend xfs (Factors ys)

(Excuse me, I can’t modify previous post)

In C version, we can reduce main loop from “i = n” to “i = n >> 1″.

That version has O(n log log n) and are not needed primes calculation nor mul nor div operations (space is O(n)).

Did in Go using my Project Euler library. Since it’s designed to solve PE problems everything is done with type uint64.

That’s a fun little problem. Here is my solution. It is quick and dirty, not efficient.

Assuming `primes` is already implemented, returning a stream of prime numbers in order, in Haskell,

Another Python version. The optimization suggested by Will Ness did not work (in Python).