9

For project Euler 148 problem, I want to get the amount of numbers in Pascals Triangle that are not divisible by 7 in row 0 to n where n is $10^9$.

Find the number of entries which are not divisible by 7 in the first one billion (109) rows of Pascal's triangle.

I did that by iterating all numbers from 0 to n. For each iteration, convert n to base 7, add 1 to each digit and multiply them after that. This gives me all the numbers that are not divisible by 7 in that row. I add the result then to the total sum.

The question I have, is there another method that doesn't require the iteration of all the numbers from 0 to n? I found an article that does it somehow by making use of triangular numbers:

Take a look at $10^9$ in base 7: $10_{10}^9=33531600616_7$

If our base 7 number was actually 30000000000, then all would need to do is calculate $T3$, then multiply by $28^n$, where n is the number of digits after the digit in question (in this case, there are 10 following digits). The 28 comes from $T7$, which arises from each digit effectively contributing the sum of 1 to 7. Thus, if 148 posed the problem for $30000000000_7$ rows, the answer would be $T3×28^{10}=1777180600172544$.

However, our problem is slightly more complicated, as there are other digits. Move onto the next one: $33000000000_7$ We add our previous result to this one ($T3×28^9$), but we need to incorporate the fact that we’ve “added onto” the most significant digit. This is done by simply multiplying this digit’s contribution by all previous digits plus 1, like so: [...]

But I don't really understand how it was done. How can you make use of the triangular numbers to find the sum of numbers not divisible by 7 in all rows of Pascals Triangle up to row $10^9$.

EDIT

I created a program that outputs the first 35 rows of the triangle and highlights numbers that are divisible by 7.

0   [0]
1   [0,0]
2   [0,0,0]
3   [0,0,0,0]
4   [0,0,0,0,0]
5   [0,0,0,0,0,0]
6   [0,0,0,0,0,0,0]
7   [0,1,1,1,1,1,1,0]
8   [0,0,1,1,1,1,1,0,0]
9   [0,0,0,1,1,1,1,0,0,0]
10  [0,0,0,0,1,1,1,0,0,0,0]
11  [0,0,0,0,0,1,1,0,0,0,0,0]
12  [0,0,0,0,0,0,1,0,0,0,0,0,0]
13  [0,0,0,0,0,0,0,0,0,0,0,0,0,0]
14  [0,1,1,1,1,1,1,0,1,1,1,1,1,1,0]
15  [0,0,1,1,1,1,1,0,0,1,1,1,1,1,0,0]
16  [0,0,0,1,1,1,1,0,0,0,1,1,1,1,0,0,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0,0]

Including the row number 33 in that diagram, I can see that there are 10 triangles there, which have $21 = T6$ ones. What I don't understand is how this helps getting the sum of all the numbers divisible by 7, because each triangle contains different numbers.

BMBM
  • 2,715
  • Maybe it is simpler to sum the numbers that are divisible by $7$? – Hagen von Eitzen Apr 17 '17 at 09:23
  • @HagenvonEitzen can you explain a bit more? Like, I should try to find the amount of triangular numbers in $10^9$? – BMBM Apr 17 '17 at 09:47
  • I think he means, to find the sum of numbers not divisible by $7$, we only need to find the sum of all numbers (which is easy) minus the sum of the numbers divisible by $7$. This may be easier because there are "less" numbers divisible by $7$ than not –  Apr 17 '17 at 10:19
  • @vrugtehagel I see, I think I get the general idea. I can see how I can sum n rows of Pascals triangle by using powers of two, but can you give me a hint how to sum all the numbers divisible by 7? It doesn't seem straightforward as there are a different amount of powers of 7 per row (or none). – BMBM Apr 18 '17 at 01:00
  • I agree with you - I don't see how it's easier either theoretically, I was simply explaining what I thought @HagenvonEitzen meant. I can however definitely be useful if you're going to use a computer to calculate this. Also, that's quite a nice table! I've done this before for a different purpose and I can tell you this behaves like a fractal. There's $7$ rows of the bigger triangles and then there's an even bigger triangle (starting in the $49$'th row). At every power of $7$, a new, bigger triangle occurs –  Apr 18 '17 at 10:11
  • Correction - I just now see I wrote "I can however definitely ..." rather than "It can however definitely ...". –  Apr 18 '17 at 20:48
  • @vrugtehagel Thanks, I generated the output with a small program in Julia (https://julialang.org/), which I also use to solve other project Euler problems. It would be cool if someone could just give me some hint, I don't expect any ready-made solution (as I already have a solution). I just want to write a faster program that doesn't need to iterate from 1 to 10^9 – BMBM Apr 19 '17 at 01:00
  • I'm still thinking about this, but I want to to share a paper that I wrote with a couple others about the amount of zeroes in pascal's triangle $\mod p$ (where $p$ is prime). Since that is very related to this, I thought I should share it: here it is –  Apr 19 '17 at 07:40
  • Does this question miss something or is it against the site rules to post questions related to project euler? It's a bit frustrating to just get a very small hint and then no activity at all anymore, I would at least like to understand the reason, as I'm quite new to this community. – BMBM Apr 25 '17 at 04:30
  • @vrugtehagel Thanks for the paper and your efforts so far! – BMBM Apr 25 '17 at 04:31
  • This is a relatively hard question for this website. There's nothing wrong with the question. You could try to draw more attention to it by offering a bounty –  Apr 25 '17 at 12:51
  • I was wondering why it is hard, I basically included two possible solutions, I wanted to have an explanation for the one that doesn't need iteration, as the explanation given in the blog post doesn't cut it for me. If offering internet points helps, sure why not. – BMBM Apr 26 '17 at 01:01
  • I think it may be beneficial for you to read this post which includes some thoughts from the creator of project euler: https://math.meta.stackexchange.com/a/21390/26369 – Mark S. Apr 26 '17 at 02:10
  • 2
    Yeah I read that and I don't think it applies to me. – BMBM Apr 26 '17 at 02:11
  • @Max I think you need to clarify. PE 148 asks for the number of entries not divisible by seven, not the sum. – AlgorithmsX Apr 28 '17 at 00:20
  • https://en.wikipedia.org/wiki/Lucas%27s_theorem – Aryabhata Apr 28 '17 at 21:38
  • @AlgorithmsX Yes I clarified what I meant, I mixed up sum and amount in the question. – BMBM May 02 '17 at 01:38
  • I’m voting to close this question because this is part of a current contest, which we do not work on. – Ross Millikan Aug 17 '20 at 03:06

3 Answers3

2

Assuming you do not need rigorous, mathematical justification, you can simply do it in the following way:

First, find $n = \dfrac{10^9}{7}=142857142$. Then there will be in total of $ T_n = \dfrac{n(n+1)}{2}$ triangular block of consisting of $1's$. As you demonstrated in your code, each of those block has exactly $T_6 = 21$ $1's$, so there are $N = 21T_n$ numbers that are divisible by $7$ in the first $10^9$ rows of the Pascal triangle. Once you have found this, the answer to actual problem will simply be $1+2+...+10^9 - N$, obviously.

Last but not least, notice that how the only rows that has no $1's$ are the rows numbered $6, 13, 20,...$. In fact, this pattern continues and $10^9$ th row will precisely be another zero row because $10^9= -1\mod 7.$ Therefore, you do not need to worry about half triangle appearing at the very bottom. I am certain that the number $10^9$ was chosen for this particular purpose.

One can prove all these assertions mathematically, but you need at least Lucas's theorem, or some equivalent machinery which in general assumes a very decent knowledge of number theory. The author of that article you mentioned did a very poor job of attempting explain the reasoning.

dezdichado
  • 14,761
  • 2
  • 26
  • 56
  • I give you the bounty because I found your explanation the most clear one, as compared to the one in the article. I would still be interested in the mathematical justification. :) – BMBM May 03 '17 at 01:15
  • 1
    This approach only works until row 48 (when counting the rows from 0). After that the pattern with the triangle repeats in itself. Looks pretty cool, but makes this idea not usable). – BMBM May 15 '17 at 08:22
0

This is only some simplified ideas.

  1. The sum of elements of $m$-th string is $2^m,$ so the total sum for the first $m$ strings is $2^{m+1} - 2.$
  2. When subtracting the "extra" elements of a triangle, the relations $$\genfrac{(}{)}{0}{}{m}{k} + \genfrac{(}{)}{0}{}{m}{k+1} = \genfrac{(}{)}{0}{}{m+1}{k+1}$$ can be used, which reduce the amount of operations by half.
  3. Recurrent calculations can be implemented due to formulas $$\genfrac{(}{)}{0}{}{m}{k+2} = \genfrac{(}{)}{0}{}{m}{k}\cdot\dfrac{(k+1)(k+2)}2,$$ $$\genfrac{(}{)}{0}{}{m+1}{k} = \genfrac{(}{)}{0}{}{m}{k} \cdot\dfrac{m+1}{m+1-k}.$$
  4. Also the mirror symmetry in rows can be used.

enter image description here

But I'm not sure that it can help in the general task.

  • Yuri, thanks for your input. My question title was ambigious, I was actually interested in the amout of numbers not divisible by 7, but without iteration. – BMBM May 02 '17 at 01:39
0

As vrugtehagel pointed out, the Pascal triangle $\mod p$ has a fractal structure. If you look at it long and strong enough, you can deduce the formula mentioned in Aryabhata's comment. (It can be extended to $\mod {p^n}$, and to $\mod n$ through the Chinese Reminder Theorem.)

Because it is a fractal you can deduce a formula for the number of non-zero-reminders at any "level" of the fractal...

Pablo H
  • 550