Distinct Primes Factors (PE47)

On Project Euler, the 47th problem is called Distinct Primes Factors.

This is a good example of why Number Theory is considered to be a bit silly at times. We are looking at the first two consecutive numbers with two distinct prime factors. Then the first three consecutive numbers with three distinct prime factors. Our task here is to find the first four consecutive numbers with four prime factors.

We'll rely on the Fundamental Theorem of Arithmetic since we know that for every composite number, there must be a unique product of prime powers.

Since I know this pattern must exist, I can just write some code to just keep processing until we find it. We'll start from the first distinct combination of four primes, \(2 \cdot 3 \cdot 5 \cdot 7\).

using Primes

function countfactors(N)
    factor(N).pe |> length
end

N = 2 * 3 * 5 * 7

while true
    if countfactors(N) == countfactors(N+1) == countfactors(N+2) == countfactors(N+3) == 4
        println(N)
        break
    end
    N += 1
end

>  0.185312 seconds (1.18 M allocations: 56.822 MiB, 9.52% gc time, 2.26% compilation time)
> 134043