## Smallest Consecutive Four-Factor Composites

### September 17, 2013

There are two ways to solve this problem. One is to factor each number starting from 2, count the number of distinct prime factors, and stop when you find the desired set (the acronym fff stands for “first four factor”):

`(define (factors n)`

(let ((wheel (vector 1 2 2 4 2 4 2 4 6 2 6)))

(let loop ((n n) (f 2) (w 0) (fs (list)))

(cond ((< n (* f f))

(reverse (if (< 1 n) (cons n fs) fs)))

((zero? (modulo n f))

(loop (/ n f) f w (cons f fs)))

(else (loop n (+ f (vector-ref wheel w))

(if (< w 10) (+ w 1) 0) fs)))))))

`(define (count n) (length (unique = (factors n))))`

`(define (fff1)`

(let loop ((n 5) (c1 1) (c2 1) (c3 1))

(let ((c (count n)))

(if (= 4 c c1 c2 c3) (- n 3)

(loop (+ n 1) c c1 c2)))))

`> (time (fff1))`

(time (fff1))

63 collections

1389 ms elapsed cpu time, including 220 ms collecting

1388 ms elapsed real time, including 208 ms collecting

531500752 bytes allocated, including 539500144 bytes reclaimed

134043

A better approach uses sieving. Instead of starting with each sieve item `#t`

and changing it to `#f`

when it is a multiple of a prime, start with each sieve item 0 and add 1 each time a prime multiple hits that element of the sieve, then look for four adjacent 4-counts:

`(define (fff2 n)`

(let ((sieve (make-vector n 0)))

(do ((p 2 (+ p 1))) ((<= n p))

(when (zero? (vector-ref sieve p))

(do ((i p (+ i p))) ((<= n i))

(vector-set! sieve i

(+ (vector-ref sieve i) 1)))))

(let loop ((i 4))

(if (= i n)

"failed"

(if (and (= (vector-ref sieve (- i 3)) 4)

(= (vector-ref sieve (- i 2)) 4)

(= (vector-ref sieve (- i 1)) 4)

(= (vector-ref sieve i) 4))

(- i 3)

(loop (+ i 1)))))))

`> (time (fff2 135000))`

(time (fff2 135000))

1 collection

62 ms elapsed cpu time, including 0 ms collecting

56 ms elapsed real time, including 3 ms collecting

3495248 bytes allocated, including 10278656 bytes reclaimed

134043

That’s a twenty-fold improvement. Of course, I was able to minimize sieving time because I already knew the answer. In practice, it would take a little bit longer because I would start with a small *n* and work my way up; start with 10000, say, and proceed by a sequence of doublings: 20000, 40000, 80000, 160000, bingo!

We used `unique`

from the Standard Prelude. You can run the program at http://programmingpraxis.codepad.org/ZVI08RRU.

Now try finding the smallest run of three consecutive numbers with exactly one prime factor. ;-)

Well, that didn’t quite work. Let’s try that again.

Related mathoverflow thread:

http://mathoverflow.net/questions/52417/consecutive-numbers-with-n-prime-factors

A version in Python. The generator fac_gen generates pairs (i, number of distinct factors of i) and is in principle unlimited, as long as it fits in memory. It works up to n=5.

A minor optimization to the posted fff2 which, instead of always incrementing the search loop by 1, skips ahead by up to 4 places based upon the values seen so far (inspired by KMP string search):

Under Kawa on my laptop, I see results like

Execution of (fff1) took 1995.53 ms

134043

Execution of (fff2 135000) took 87.817 ms

134043

Execution of (fff3 135000) took 66.956 ms

134043

Oops, that should be (let ((n-4 (- n 4))) … (if (> i n-4) …)) otherwise there can be an index out of bounds error.

I decided to see what kind of improvement there would be adding type info and using a primitive int array rather than a vector — this is still with Kawa — and it turns out to be quite substantial (another factor of 10):

Execution of (fff4 135000) took 6.586 ms

134043

A version of

`fff2`

in C++11, making use of my library that (sort of) implements Haskell’s`Maybe`

:Straightforward Racket version:

I also did a sieved version and one for n consecutive numbers with m total distinct prime factors (since that’s how I originally read the problem :) on my blog: jverkamp.com: Smallest Consecutive Four-Factor Composites