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 May 21, 2026
Problem #0148
Level Level 06
Solved By 5,954
Languages C++, Python
Answer 2129970655314432
Length 336 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 Development

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, ki≤nik_i \le n_i.

Counting Non-Divisible Entries in Row nn

For a given row nn with base-7 representation n=(dsds−1⋯d1d0)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=0N−1∏i(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=(dsds−1⋯d1d0)7N = (d_s d_{s-1} \cdots d_1 d_0)_7.

Consider the decomposition at digit position ii: N=di⋅7i+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)2⋅28i+(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):

result←di(di+1)2⋅28i+(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 7k−17^k - 1:

S(7k)=∏i=0k−1∑di=06(di+1)=∏i=0k−128=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).

Editorial

Lucas’ theorem turns the divisibility question into a digit-by-digit counting problem in base 7. Once row nn is written in base 7, the number of entries in that row not divisible by 7 is simply the product of one plus each digit. So the global task is no longer about Pascal’s triangle directly; it becomes a digit-DP style summation over all rows from 0 up to 109−110^9-1.

The implementation uses the recurrence for complete base-7 blocks. When the current digit equals dd, all smaller leading digits contribute

d(d+1)2⋅28i,\frac{d(d+1)}{2}\cdot 28^i,

and the rows sharing that same leading digit contribute an extra factor of (d+1)(d+1) times the result from the already processed lower part. Processing the base-7 digits from least significant to most significant therefore builds the answer in logarithmic time.

Pseudocode

Write $10^9$ in base 7, keeping the digits from least significant to most significant.

Initialize:
    the answer as 0,
    the current block weight as $28^0=1$.

For each base-7 digit $d$ in that order:
    Add the contribution of all rows whose current digit is smaller than $d$.
    Then scale the partial answer by $(d+1)$ to account for rows whose current digit matches $d$.
    Advance the block weight from $28^i$ to $28^{i+1}$.

After the final digit has been processed, return the accumulated answer.

Complexity Analysis

  • Converting NN to base 7: O(log⁡7N)O(\log_7 N) digits.
  • Iterative computation: O(log⁡7N)O(\log_7 N) steps.
  • Total: O(log⁡N)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;
}