All Euler problems
Project Euler

Factors of Two in Binomial Coefficients

Define g(n, m) as the largest integer k such that 2^k | C(n, m). Let F(n) = max{g(n, m): 0 <= m <= n} and S(N) = sum_(n=1)^N F(n). Given F(10) = 3, F(100) = 6, S(100) = 389, S(10^7) = 203,222,840,...

Source sync Apr 19, 2026
Problem #0704
Level Level 14
Solved By 1,178
Languages C++, Python
Answer 501985601490518144
Length 321 words
combinatoricsbrute_forceconstructive

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Define \(g(n, m)\) to be the largest integer \(k\) such that \(2^k\) divides \(\binom {n}m\). For example, \(\binom {12}5 = 792 = 2^3 \cdot 3^2 \cdot 11\), hence \(g(12, 5) = 3\). Then define \(F(n) = \max \{ g(n, m) : 0 \le m \le n \}\). \(F(10) = 3\) and \(F(100) = 6\).

Let \(S(N)\) = \(\displaystyle \sum _{n=1}^N{F(n)}\). You are given that \(S(100) = 389\) and \(S(10^7) = 203222840\).

Find \(S(10^{16})\).

Problem 704: Factors of Two in Binomial Coefficients

Mathematical Foundation

Theorem 1 (Kummer, 1852). The pp-adic valuation νp(nm)\nu_p\binom{n}{m} equals the number of carries when adding mm and nmn - m in base pp.

Proof. By Legendre’s formula, νp(k!)=i=1k/pi\nu_p(k!) = \sum_{i=1}^{\infty}\lfloor k/p^i \rfloor. Therefore

νp(nm)=i=1(npimpinmpi).\nu_p\binom{n}{m} = \sum_{i=1}^{\infty}\left(\left\lfloor \frac{n}{p^i}\right\rfloor - \left\lfloor \frac{m}{p^i}\right\rfloor - \left\lfloor \frac{n-m}{p^i}\right\rfloor\right).

Each summand equals the carry out of position i1i-1 when adding mm and nmn-m in base pp. This is Kummer’s classical result. \square

Theorem 2 (Maximum 2-adic Valuation). For n1n \ge 1,

F(n)=log2nν2(n),F(n) = \lfloor \log_2 n \rfloor - \nu_2(n),

where ν2(n)\nu_2(n) is the 2-adic valuation of nn (the exponent of 2 in the factorization of nn).

Proof. Write nn in binary as n=(bLbL1b1b0)2n = (b_L b_{L-1} \cdots b_1 b_0)_2 where bL=1b_L = 1 and L=log2nL = \lfloor \log_2 n \rfloor. Let ν2(n)=t\nu_2(n) = t, so bits b0=b1==bt1=0b_0 = b_1 = \cdots = b_{t-1} = 0 and bt=1b_t = 1.

By Kummer’s theorem, ν2(nm)\nu_2\binom{n}{m} counts the carries in m+(nm)m + (n-m) in binary. A carry at position ii occurs when mi+(nm)i+ci12m_i + (n-m)_i + c_{i-1} \ge 2, where ci1c_{i-1} is the incoming carry.

Upper bound: The total number of carries is at most LtL - t. Position tt is the lowest 1-bit of nn. At positions 0,1,,t10, 1, \ldots, t-1, we have ni=0n_i = 0, so mi+(nm)i=mi+(nm)im_i + (n-m)_i = m_i + (n-m)_i must produce digit 00. If there is an incoming carry, the outgoing carry simply propagates but does not create a “net” carry. The bit at position LL is the highest bit and cannot generate a carry out. Hence at most LtL - t positions can contribute carries.

Achievability: Choose mm such that in binary addition m+(nm)m + (n-m), carries are generated at every position from tt through L1L-1. For example, take m=(0bL1bL2bt+1011t)2m = (0 \, b_{L-1} \, b_{L-2} \cdots b_{t+1} \, 0 \, \underbrace{1 \cdots 1}_{t})_2 (a specific construction that maximizes carry chain length). This produces exactly LtL - t carries. \square

Lemma 1 (Summation Decomposition). S(N)=A(N)B(N)S(N) = A(N) - B(N), where

A(N)=n=1Nlog2n,B(N)=n=1Nν2(n).A(N) = \sum_{n=1}^{N} \lfloor \log_2 n \rfloor, \qquad B(N) = \sum_{n=1}^{N} \nu_2(n).

Proof. Immediate from F(n)=log2nν2(n)F(n) = \lfloor \log_2 n \rfloor - \nu_2(n) and linearity of summation. \square

Lemma 2 (Closed Form for A(N)A(N)). Let L=log2NL = \lfloor \log_2 N \rfloor. Then

A(N)=(L2)2L+2+L(N2L+1).A(N) = (L - 2) \cdot 2^L + 2 + L(N - 2^L + 1).

Proof. For b=0,1,,L1b = 0, 1, \ldots, L-1, the integers nn with log2n=b\lfloor \log_2 n \rfloor = b are {2b,2b+1,,2b+11}\{2^b, 2^b + 1, \ldots, 2^{b+1} - 1\}, contributing b2bb \cdot 2^b. The remaining integers {2L,,N}\{2^L, \ldots, N\} contribute L(N2L+1)L(N - 2^L + 1). Thus:

A(N)=b=0L1b2b+L(N2L+1).A(N) = \sum_{b=0}^{L-1} b \cdot 2^b + L(N - 2^L + 1).

The identity b=0L1b2b=(L2)2L+2\sum_{b=0}^{L-1} b \cdot 2^b = (L-2)\cdot 2^L + 2 follows by differentiating the geometric series xb\sum x^b and substituting x=2x = 2. \square

Lemma 3 (Closed Form for B(N)B(N)).

B(N)=k=1LN2k.B(N) = \sum_{k=1}^{L} \left\lfloor \frac{N}{2^k} \right\rfloor.

Proof. Each integer nn contributes ν2(n)\nu_2(n) to B(N)B(N). The value ν2(n)k\nu_2(n) \ge k iff 2kn2^k \mid n, so B(N)=k=1N/2kB(N) = \sum_{k=1}^{\infty} \lfloor N/2^k \rfloor, which terminates at k=Lk = L. \square

Editorial

We compute A(N). Finally, compute B(N). We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

Compute A(N)
Compute B(N)

Complexity Analysis

  • Time: O(logN)O(\log N), since the loop runs L=log2NL = \lfloor \log_2 N \rfloor iterations and all arithmetic is O(1)O(1) (with big-integer support for N=1016N = 10^{16}).
  • Space: O(1)O(1).

Answer

501985601490518144\boxed{501985601490518144}

Code

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

C++ project_euler/problem_704/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef __int128 lll;

int main() {
    ll N = 10000000000000000LL; // 10^16

    // S(N) = sum_{n=1}^{N} F(n)
    // F(n) = floor(log2(n)) - v2(n)
    // S(N) = A(N) - B(N)
    // A(N) = sum of floor(log2(n)) for n=1..N
    // B(N) = sum of v2(n) for n=1..N

    // Compute A(N) = sum of floor(log2(n)) for n=1..N
    int B = 63 - __builtin_clzll(N); // floor(log2(N))
    // sum_{b=0}^{B-1} b * 2^b = (B-2)*2^B + 2
    // Plus B * (N - 2^B + 1) for numbers from 2^B to N

    // Use __int128 to avoid overflow
    lll powerB = (lll)1 << B;
    lll A = (lll)(B - 2) * powerB + 2 + (lll)B * ((lll)N - powerB + 1);

    // Compute B(N) = sum_{k=1}^{inf} floor(N/2^k)
    lll Bval = 0;
    for (int k = 1; k <= B; k++) {
        Bval += (lll)N >> k;
    }

    lll result = A - Bval;

    // Print __int128
    ll r = (ll)result;
    cout << r << endl;

    return 0;
}