Monday 27 May 2019

Project Euler 148 - Exploring Pascal's triangle

We can easily verify that none of the entries in the first seven rows of Pascal's triangle are divisible by 7:
 1
 1  1
 1  2  1
 1  3  3  1
 1  4  6  4  1
 1  5 10 10  5  1
1  6 15 20 15  6  1
However, if we check the first one hundred rows, we will find that only 2361 of the 5050 entries are not divisible by 7.
Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle.

Solution

Rewrite the number N in the format of:

N = q1 * 7p1 + q2 * 7p2 + q3 * 7p3 .... + qk-1 * 7 + qk

For example:

1934 = 343 * 5 + 49 * 4 + 7 * 3 + 2

Define PA(N) as the function to find the number of entries which are not divisible by 7 in the first N rows of Pascal's triangle.

PA(q1 * 7p1) = 28p1 * (q1 + 1) * q1 / 2

For example:

PA(5 * 73) = 283 * 6 * 5 / 2 = 329280

The final formula

PA(q1 * 7p1 + q2 * 7p2 + q3 * 7p3 .... + qk-1 * 7 + qk) = PA(q1 * 7p1) + (p1 + 1) * PA(q2 * 7p2) + (p1 + 1)*(p2 + 1)*PA(q3 * 7p3) + .....

For example:

PA(1934) = PA(343 * 5 + 49 * 4 + 7 * 3 + 2) = PA(345 * 5) + (5+1) * PA(49 * 4) + (5+1)*(4+1)*PA(7*3) + (5+1)*(4+1)*(3+1)*PA(2)=381720

I don't know how to prove it.