## Project Euler 12

### March 29, 2019

Let’s start with a function to find the factors of a number *n*:

(define (factors n) ; 2,3,5-wheel (let ((wheel (vector 1 2 2 4 2 4 2 4 6 2 6))) (let loop ((n n) (f 2) (w 0) (fs (list))) (if (< n (* f f)) (reverse (cons n fs)) (if (zero? (modulo n f)) (loop (/ n f) f w (cons f fs)) (loop n (+ f (vector-ref wheel w)) (if (= w 10) 3 (+ w 1)) fs))))))

We use a method called wheel factorization; at O(sqrt *n*), it’s not the fastest factoring algorithm available, but is sufficient for Project Euler. Next we use the factorization of *n* to determine the number of divisors of *n* by counting multiplicities of factors:

(define (numdiv n) (let ((fs (factors n))) (let loop ((prev (car fs)) (fs (cdr fs)) (f 2) (d 1)) (cond ((null? fs) (* d f)) ((= (car fs) prev) (loop prev (cdr fs) (+ f 1) d)) (else (loop (car fs) (cdr fs) 2 (* d f)))))))

Now we can generate the triangle numbers, count the divisors of each, and stop when we’ve seen enough divisors:

(define (euler12 limit) (let loop ((n 1) (tri 1)) (if (< limit (numdiv tri)) (list n tri (numdiv tri)) (loop (+ n 1) (+ tri n 1)))))

On my system, using Chez Scheme, that finds the answer in about a fifth of a second:

> (time (euler12 500)) (time (euler12 500)) 13 collections 203 ms elapsed cpu time, including 0 ms collecting 208 ms elapsed real time, including 0 ms collecting 55714592 bytes allocated, including 54737696 bytes reclaimed (12375 XXXXXXXX 576)

In the spirit of Project Euler, I elided the answer. You can run the program at https://ideone.com/LDfhXa.

Advertisements

I did many of the Euler problems some years ago.

I posted the code on ideone.

A naive divisor counting approach works better if we make use of the fact that triangular numbers are of the form n*(n+1)/2: so just need to count the divisors of n and 2n+1 or 2n-1:

Takes just a few seconds to get to 500 divisors.

A Haskell version.

A Julia version…

function NumberOfDivisors(n::Int64)

m = n

c = 2

d = n / 2

d_ = round(Int64, d)

end

function main(n::Int64 = 1000000)

x = 0

nd = 0

end

The script can be remedied so as to cover larger integers (BigInt type), if needed. Although somewhat slower in that case, it is still fast enough to be practical.

Another Algorithm:

We need to calculate the number of divisors of n(n+1)/2. This number is the product of two

numbers, one of which is even. Divide the even one by 2. Now we find the number of

divisors of the product n1 * n2. This can be done by finding the factors of n1 and n2, merging

them, then calculating the number of divisors from the number of each factor. But it should

be noted that as we scan through the values of n, we may have already calculated the factors

of the smaller of n1 and n2 and sometimes even the factors of the larger. Therefore we maintain

a list of the factors of each n we have seen.

This means we shall factor roughly the same number of n’s that we calculated for n(n+1)/2 but

each one we calculate is smaller (roughly = sqrt(n(n+1)/2) ) than the product n(n+1)/2. Thus it should be faster than

calculating the divisors of n(n+1)/2. On my computer it is 7X faster (21 msec).

in C#

static void Main(string[] args)

{

Stopwatch s = new Stopwatch();

int N = 15000;

List[] faclist = new List[15000];

s.Start();

for(int i = 0; i < N; i++)

faclist[i] = new List();

for(int i = 2; i < N; i++)

{

int n1 = i;

int n2 = i+1;

if((n1 & 1) == 0) //even?

n1 /= 2;

else

n2 /= 2;

if(faclist[n1].Count == 0)

faclist[n1] = factor(n1);

if(faclist[n2].Count == 0)

faclist[n2] = factor(n2);

if(numdivisors(merge(faclist[n1], faclist[n2])) > 500)

{

Console.WriteLine(“{0} {1}”, i, n1 * n2);

break;

}

}

s.Stop();

Console.WriteLine(s.Elapsed);

} //end Main

@Ernie: note that the two factors a,b of a triangular number t = ab = n(n+1)/2 are either n/2, n+1 or n, (n+1)/2 and since n and n+1 must be coprime, so must a and b. My solution above uses that fact as does this Rust program, which uses a sieve to efficiently find the number of divisors of 2..N (I’m just starting with Rust so may not be stylistically optimal, improvements welcome):

Looking at the first 1000000 triangular numbers tells us eg. that the 7289919th triangular number, 26571463158240, has 7680 factors, which as it’s a record breaker, appears in https://oeis.org/A076711, “Highly Composite Triangular Numbers” (at position 43: https://oeis.org/A076711/b076711.txt).

Thank you Matthew for that observation. I only need to change one line of code (eliminate the merge) and the programme becomes 40% faster.