1

Project Euler

Like the last problem I find it’s informative to start out with an inelegant solution and try to find an elegant one at the end.

We start by generating an array of the form a^b from a = 2 to 100 and b = 2 to 100.

2

Problem 47

The first two consecutive numbers to have two distance prime factors are:

14=2×7

15=3×5

The first three consecutive numbers to have three distinct prime factors are:

644=2^2×7×23 644=2^2×7×23 644=2^2×7×23

Find the first four consecutive numbers to have four distinct prime factors. What is the first of these numbers?

prob47 = Flatten[Table[Length[FactorInteger[n]], {n, 1, 200000}]];
prob47b = Table[Sum[prob47[[a + i]], {i,0,3}], {a, 1, 199997}];
Position[prob47b, 16]

Problem 44

Pentagonal numbers are generated by the formula, P_n=n(3n-1)/2. The first ten pentagonal numbers are:

1, 5, 12, 22, 35, 51, 70, 92, 117, 145,

It can be seen that P_4 + P_7 = 22 + 70 = 92 = P_8. However, their difference, 70 - 22 = 48, is not pentagonal.

Find the pair of pentagonal numbers, P_j and P_k, for which their sum and difference are pentagonal and D = |P_k - P_j| is minimized; what is the value of D?

prob44a = Table[m*(m + 1)/2, {m, 1, 100000}];
prob44b = Table[n*(3*n-1)/2, {n, 1, 100000}];
prob44c = Table[o*(2*o-1), {o, 1, 100000}];
Intersection[prob44a, prob44b, prob44c]

Problem 40

An irrational decimal fraction is created by concatenating the positive integers:

0.123456789101112131415161718192021...

It can be seen that the 12^th digit of the fractional part is 1.

If d_n represents the n^th digit of the fractional part, find the value of the following expression.

d_1 × d_{10} × d_{100} × d_{1000} × d_{10000} × d_{100000} × d_{1000000}

prob40 = Flatten[IntegerDigits[Table[n, {n, 1, 200000}]]];
Product[prob40[[10^a]], {a, 0, 6}]