All Euler problems
Project Euler

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.)

Source sync Apr 19, 2026
Problem #0148
Level Level 06
Solved By 5,954
Languages C++, Python
Answer 2129970655314432
Length 210 words
geometryrecursionmodular_arithmetic

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 not divisible by \(7\).

Find the number of entries which are not divisible by \(7\) in the first one billion (\(10^9\)) rows of Pascal’s triangle.

Problem 148: Exploring Pascal’s Triangle

Mathematical Analysis

Lucas’ Theorem

By Lucas’ theorem, a binomial coefficient (nk)\binom{n}{k} is not divisible by a prime pp if and only if every digit of kk in base pp is less than or equal to the corresponding digit of nn in base pp.

For prime p=7p = 7: (nk)≢0(mod7)\binom{n}{k} \not\equiv 0 \pmod{7} iff for each base-7 digit position ii, kinik_i \le n_i.

Counting Non-Divisible Entries in Row nn

For a given row nn with base-7 representation n=(dsds1d1d0)7n = (d_s d_{s-1} \cdots d_1 d_0)_7, the number of entries (nk)\binom{n}{k} not divisible by 7 is:

i=0s(di+1)\prod_{i=0}^{s} (d_i + 1)

This is because for each digit position, kik_i can independently take any value from 0 to did_i.

Summing Over All Rows

We need:

S(N)=n=0N1i(di(n)+1)S(N) = \sum_{n=0}^{N-1} \prod_{i} (d_i(n) + 1)

where N=109N = 10^9 and di(n)d_i(n) are the base-7 digits of nn.

Recursive Formula

Write NN in base 7: N=(dsds1d1d0)7N = (d_s d_{s-1} \cdots d_1 d_0)_7.

Consider the decomposition at digit position ii: N=di7i+RN = d_i \cdot 7^i + R where RR represents the lower-order digits.

The recursion, processed from the least significant digit to the most significant:

S(N)=di(di+1)228i+(di+1)S(Rhigher)S(N) = \frac{d_i(d_i+1)}{2} \cdot 28^i + (d_i+1) \cdot S(R_{\text{higher}})

where RhigherR_{\text{higher}} is the number formed by the higher-order digits.

Iteratively, starting with result=0\text{result} = 0 and processing digits from d0d_0 (LSB) to dsd_s (MSB):

resultdi(di+1)228i+(di+1)result\text{result} \leftarrow \frac{d_i(d_i+1)}{2} \cdot 28^i + (d_i+1) \cdot \text{result}

Why S(7k)=28kS(7^k) = 28^k

For a complete set of 7k7^k rows: each row nn with kk base-7 digits contributes (di+1)\prod(d_i+1). Summing over all nn from 0 to 7k17^k - 1:

S(7k)=i=0k1di=06(di+1)=i=0k128=28kS(7^k) = \prod_{i=0}^{k-1} \sum_{d_i=0}^{6} (d_i + 1) = \prod_{i=0}^{k-1} 28 = 28^k

Base-7 Representation of 10910^9

109=(3  3  5  3  1  6  0  0  6  1  6)710^9 = (3\; 3\; 5\; 3\; 1\; 6\; 0\; 0\; 6\; 1\; 6)_7 (MSB to LSB).

Complexity

  • Converting NN to base 7: O(log7N)O(\log_7 N) digits.
  • Iterative computation: O(log7N)O(\log_7 N) steps.
  • Total: O(logN)O(\log N).

Answer

2129970655314432\boxed{2129970655314432}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_148/solution.cpp
#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;
}