Exploring Pascal's Triangle
How many entries in the first 10^9 rows of Pascal's triangle are not divisible by 7? (Rows are numbered starting from row 0 at the top.)
Problem Statement
This archive keeps the full statement, math, and original media on the page.
We can easily verify that none of the entries in the first seven rows of Pascal’s triangle are divisible by \(7\): \[ \begin {array}{ccccccccccccccc} & & & & & & 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 & & \\ \end {array} \]
However, if we check the first one hundred rows, we will find that only \(2361\) of the \(5050\) entries are
Find the number of entries which are
Problem 148: Exploring Pascal’s Triangle
Mathematical Analysis
Lucas’ Theorem
By Lucas’ theorem, a binomial coefficient is not divisible by a prime if and only if every digit of in base is less than or equal to the corresponding digit of in base .
For prime : iff for each base-7 digit position , .
Counting Non-Divisible Entries in Row
For a given row with base-7 representation , the number of entries not divisible by 7 is:
This is because for each digit position, can independently take any value from 0 to .
Summing Over All Rows
We need:
where and are the base-7 digits of .
Recursive Formula
Write in base 7: .
Consider the decomposition at digit position : where represents the lower-order digits.
The recursion, processed from the least significant digit to the most significant:
where is the number formed by the higher-order digits.
Iteratively, starting with and processing digits from (LSB) to (MSB):
Why
For a complete set of rows: each row with base-7 digits contributes . Summing over all from 0 to :
Base-7 Representation of
(MSB to LSB).
Complexity
- Converting to base 7: digits.
- Iterative computation: steps.
- Total: .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
// Count entries in first N rows of Pascal's triangle not divisible by 7.
// By Lucas' theorem: C(n,k) not divisible by 7 iff each base-7 digit of k <= that of n.
// Row n contributes product(d_i + 1) where d_i are base-7 digits of n.
//
// S(N) = sum_{n=0}^{N-1} product(d_i(n)+1)
// Recursion: S(d * 7^k + R) = d*(d+1)/2 * 28^k + (d+1) * S(R)
// Process from LSB to MSB.
long long N = 1000000000LL;
// Convert N to base 7 (LSB first)
vector<int> digits;
{
long long tmp = N;
while (tmp > 0) {
digits.push_back(tmp % 7);
tmp /= 7;
}
}
// Process from LSB (i=0) to MSB
long long result = 0;
long long pow28 = 1; // 28^i
for (int i = 0; i < (int)digits.size(); i++) {
int d = digits[i];
result = (long long)d * (d + 1) / 2 * pow28 + (long long)(d + 1) * result;
pow28 *= 28;
}
cout << result << endl;
return 0;
}
"""
Problem 148: Exploring Pascal's Triangle
How many entries in the first 10^9 rows of Pascal's triangle are not divisible by 7?
By Lucas' theorem, C(n,k) is not divisible by 7 iff each base-7 digit of k <= that of n.
Row n contributes product(d_i + 1) where d_i are the base-7 digits of n.
S(N) = sum_{n=0}^{N-1} product(d_i(n) + 1)
Recursion (process from LSB to MSB):
S(d * 7^k + R) = d*(d+1)/2 * 28^k + (d+1) * S(R)
"""
def solve():
N = 10**9
# Convert N to base 7 (least significant digit first)
digits = []
tmp = N
while tmp > 0:
digits.append(tmp % 7)
tmp //= 7
# Process from LSB (i=0) to MSB
result = 0
pow28 = 1 # 28^i
for i in range(len(digits)):
d = digits[i]
result = d * (d + 1) // 2 * pow28 + (d + 1) * result
pow28 *= 28
print(result)
if __name__ == "__main__":
solve()