All Euler problems
Project Euler

Riffle Shuffles

A perfect (out-)riffle shuffle on a deck of 2n cards in positions 0, 1,..., 2n-1 maps position i to position 2i mod (2n-1) (with position 2n-1 fixed). Equivalently, working modulo m = 2n+1: positio...

Source sync May 21, 2026
Problem #0622
Level Level 10
Solved By 1,998
Languages C++, Python
Answer 3010983666182123972
Length 505 words
number_theorymodular_arithmeticlinear_algebra

Problem Statement

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

A riffle shuffle is executed as follows: a deck of cards is split into two equal halves, with the top half taken in the left hand and the bottom half taken in the right hand. Next, the cards are interleaved exactly, with the top card in the right half inserted just after the top card in the left half, the 2nd card in the right half just after the 2nd card in the left half, etc. (Note that this process preserves the location of the top and bottom card of the deck)

Let \(s(n)\) be the minimum number of consecutive riffle shuffles needed to restore a deck of size \(n\) to its original configuration, where \(n\) is a positive even number.

Amazingly, a standard deck of \(52\) cards will first return to its original configuration after only \(8\) perfect shuffles, so \(s(52) = 8\). It can be verified that a deck of \(86\) cards will also return to its original configuration after exactly \(8\) shuffles, and the sum of all values of \(n\) that satisfy \(s(n) = 8\) is \(412\).

Find the sum of all values of n that satisfy \(s(n) = 60\).

Problem 622: Riffle Shuffles

Mathematical Analysis

Connection to Multiplicative Order

The shuffle permutation sends position ii to 2i mod (2n+1)2i \bmod (2n+1). After kk shuffles, position ii goes to 2ki mod (2n+1)2^k i \bmod (2n+1). The deck returns to its original order when 2k≡1(mod2n+1)2^k \equiv 1 \pmod{2n+1} for all ii, which happens at k=ord2n+1(2)k = \text{ord}_{2n+1}(2).

s(n)=ord2n+1(2)(1)s(n) = \text{ord}_{2n+1}(2) \tag{1}

Reformulation

Setting m=2n+1m = 2n+1, we seek all odd m>1m > 1 with ordm(2)=60\text{ord}_m(2) = 60. The deck size is m−1m - 1 and we sum all such values:

Answer=∑m∣260−1m>1ordm(2)=60(m−1)(2)\text{Answer} = \sum_{\substack{m \mid 2^{60} - 1 \\ m > 1 \\ \text{ord}_m(2) = 60}} (m - 1) \tag{2}

Factorization of 260−12^{60} - 1

The condition ordm(2)=60\text{ord}_m(2) = 60 requires m∣260−1m \mid 2^{60} - 1. The complete factorization is:

260−1=1152921504606846975=32⋅52⋅7⋅11⋅13⋅31⋅41⋅61⋅151⋅331⋅1321(3)2^{60} - 1 = 1152921504606846975 = 3^2 \cdot 5^2 \cdot 7 \cdot 11 \cdot 13 \cdot 31 \cdot 41 \cdot 61 \cdot 151 \cdot 331 \cdot 1321 \tag{3}

This has (2+1)(2+1)(1+1)8=2304(2+1)(2+1)(1+1)^8 = 2304 divisors.

Verifying Exact Order

For a divisor mm of 260−12^{60}-1, we already know 260≡1(modm)2^{60} \equiv 1 \pmod{m}. The order is exactly 60 if and only if 260/p≢1(modm)2^{60/p} \not\equiv 1 \pmod{m} for every prime p∣60p \mid 60.

Since 60=22⋅3⋅560 = 2^2 \cdot 3 \cdot 5, the prime divisors are {2,3,5}\{2, 3, 5\}, and we check:

230≢1,220≢1,212≢1(modm)(4)2^{30} \not\equiv 1, \quad 2^{20} \not\equiv 1, \quad 2^{12} \not\equiv 1 \pmod{m} \tag{4}

Connection to Cyclotomic Polynomials

The prime factorization of 260−12^{60}-1 decomposes via cyclotomic polynomials:

260−1=∏d∣60Φd(2)2^{60} - 1 = \prod_{d \mid 60} \Phi_d(2)

Every prime p∣Φd(2)p \mid \Phi_d(2) has ordp(2)=d\text{ord}_p(2) = d. So the primes with order exactly 60 are precisely those dividing Φ60(2)\Phi_{60}(2).

Composite Orders via CRT

For composite m=m1m2m = m_1 m_2 with gcd⁡(m1,m2)=1\gcd(m_1, m_2) = 1:

ordm(2)=lcm(ordm1(2),ordm2(2))\text{ord}_m(2) = \text{lcm}(\text{ord}_{m_1}(2), \text{ord}_{m_2}(2))

This means composite mm with order 60 arise by combining prime powers whose individual orders have lcm=60\text{lcm} = 60. Since 60=22⋅3⋅560 = 2^2 \cdot 3 \cdot 5, valid combinations use primes whose orders divide 60 and whose lcm equals 60.

Concrete Examples of Shuffle Orders

Deck size 2n2nm=2n+1m = 2n+1ordm(2)\text{ord}_m(2)Shuffles to restore
2322
4544
6733
8966
10111010
12131212
141544
52535252
132013216060

Note: m=15=3⋅5m = 15 = 3 \cdot 5, ord15(2)=lcm(ord3(2),ord5(2))=lcm(2,4)=4\text{ord}_{15}(2) = \text{lcm}(\text{ord}_3(2), \text{ord}_5(2)) = \text{lcm}(2, 4) = 4.

Derivation

Editorial

A perfect (out-)riffle shuffle on 2n cards has order ord_{2n+1}(2). Find sum of deck sizes 2n where the shuffle order is exactly 60. Key identity: ord_m(2) = 60 iff m | 2^60 - 1 and 2^{60/p} != 1 mod m for each prime p | 60 (i.e., p in {2, 3, 5}). 2^60 - 1 = 3^2 * 5^2 * 7 * 11 * 13 * 31 * 41 * 61 * 151 * 331 * 1321 Method 1: Enumerate all divisors, check order (primary) Method 2: Compute ord_m(2) directly for small m (verification). We factorize** 260−12^{60} - 1 into prime powers: 32⋅52⋅7⋅11⋅13⋅31⋅41⋅61⋅151⋅331⋅13213^2 \cdot 5^2 \cdot 7 \cdot 11 \cdot 13 \cdot 31 \cdot 41 \cdot 61 \cdot 151 \cdot 331 \cdot 1321. We then enumerate** all 2304 divisors m>1m > 1 using Cartesian product of exponent ranges. Finally, filter**: for each mm, verify 230≢12^{30} \not\equiv 1, 220≢12^{20} \not\equiv 1, 212≢1(modm)2^{12} \not\equiv 1 \pmod{m}.

Pseudocode

Factorize** $2^{60} - 1$ into prime powers: $3^2 \cdot 5^2 \cdot 7 \cdot 11 \cdot 13 \cdot 31 \cdot 41 \cdot 61 \cdot 151 \cdot 331 \cdot 1321$
Enumerate** all 2304 divisors $m > 1$ using Cartesian product of exponent ranges
Filter**: for each $m$, verify $2^{30} \not\equiv 1$, $2^{20} \not\equiv 1$, $2^{12} \not\equiv 1 \pmod{m}$
Sum** $m - 1$ for each valid $m$

Verification

  • m=1321m = 1321: 230 mod 1321=1320≠12^{30} \bmod 1321 = 1320 \ne 1. Valid.
  • m=7m = 7: ord7(2)=3\text{ord}_7(2) = 3, so 212≡(23)4≡1(mod7)2^{12} \equiv (2^3)^4 \equiv 1 \pmod{7}. Invalid.
  • m=3⋅5⋅7=105m = 3 \cdot 5 \cdot 7 = 105: ord105(2)=lcm(2,4,3)=12≠60\text{ord}_{105}(2) = \text{lcm}(2, 4, 3) = 12 \ne 60. Invalid.

Proof of Correctness

Theorem. ordm(2)=k\text{ord}_m(2) = k if and only if 2k≡1(modm)2^k \equiv 1 \pmod{m} and 2k/p≢1(modm)2^{k/p} \not\equiv 1 \pmod{m} for every prime p∣kp \mid k.

Proof. The order is the smallest positive integer dd with 2d≡12^d \equiv 1. If d∣kd \mid k and d≠kd \ne k, then d∣k/pd \mid k/p for some prime p∣kp \mid k (since the quotient k/d>1k/d > 1 has a prime factor that also divides kk). So 2k/p≡12^{k/p} \equiv 1 for that pp. Contrapositive: if no such pp works, then d=kd = k. □\square

Proposition. For composite m=m1m2m = m_1 m_2 with gcd⁡(m1,m2)=1\gcd(m_1, m_2) = 1, ordm(2)=lcm(ordm1(2),ordm2(2))\text{ord}_m(2) = \text{lcm}(\text{ord}_{m_1}(2), \text{ord}_{m_2}(2)).

Proof. By CRT, 2k≡1(modm)2^k \equiv 1 \pmod{m} iff 2k≡1(modmi)2^k \equiv 1 \pmod{m_i} for both ii, iff ordmi(2)∣k\text{ord}_{m_i}(2) \mid k for both ii, iff lcm(ordm1(2),ordm2(2))∣k\text{lcm}(\text{ord}_{m_1}(2), \text{ord}_{m_2}(2)) \mid k. □\square

Corollary. Every valid mm is a divisor of 260−12^{60}-1, so our enumeration is exhaustive.

Proof. ordm(2)=60\text{ord}_m(2) = 60 implies m∣260−1m \mid 2^{60} - 1. □\square

Complexity Analysis

  • Divisor enumeration: O(2304)O(2304) divisors of 260−12^{60} - 1.
  • Order check: O(log⁡(260))=O(60)O(\log(2^{60})) = O(60) per divisor for modular exponentiation.
  • Total: O(2304⋅60)≈105O(2304 \cdot 60) \approx 10^5 operations.

Answer

3010983666182123972\boxed{3010983666182123972}

Code

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

C++ project_euler/problem_622/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef __int128 lll;

/*
 * Problem 622: Riffle Shuffles
 *
 * A perfect out-shuffle on 2n cards has order ord_{2n+1}(2).
 * Find sum of deck sizes 2n where ord_{2n+1}(2) = 60.
 *
 * Strategy: enumerate all divisors of 2^60 - 1, check which have
 * multiplicative order of 2 equal to exactly 60.
 *
 * 2^60 - 1 = 3^2 * 5^2 * 7 * 11 * 13 * 31 * 41 * 61 * 151 * 331 * 1321
 *
 * Two methods:
 *   1. Divisor enumeration with order check (primary)
 *   2. Direct order computation for small m (verification)
 */

// --- Fast modular exponentiation ---
ll power(ll base, ll exp, ll mod) {
    ll result = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) result = (lll)result * base % mod;
        base = (lll)base * base % mod;
        exp >>= 1;
    }
    return result;
}

// --- Check if ord_m(2) == 60 ---
// Criterion: 2^60 = 1 mod m AND 2^{60/p} != 1 mod m for p in {2,3,5}
bool has_order_60(ll m) {
    if (power(2, 60, m) != 1) return false;
    if (power(2, 30, m) == 1) return false;  // 60/2
    if (power(2, 20, m) == 1) return false;  // 60/3
    if (power(2, 12, m) == 1) return false;  // 60/5
    return true;
}

// --- Direct multiplicative order (for verification) ---
ll mult_order(ll a, ll m) {
    if (m == 1) return 1;
    ll ord = 1, cur = a % m;
    while (cur != 1) {
        cur = (lll)cur * a % m;
        ord++;
        if (ord > m) return -1;
    }
    return ord;
}

// --- Method 1: Enumerate divisors of 2^60 - 1 ---
ll solve_divisor_enum() {
    vector<pair<ll, int>> factors = {
        {3, 2}, {5, 2}, {7, 1}, {11, 1}, {13, 1},
        {31, 1}, {41, 1}, {61, 1}, {151, 1}, {331, 1}, {1321, 1}
    };
    int nf = factors.size();
    vector<int> maxexp(nf);
    for (int i = 0; i < nf; i++) maxexp[i] = factors[i].second;

    ll total = 0;
    int count = 0;

    // Iterate over all exponent combinations
    vector<int> e(nf, 0);
    while (true) {
        ll d = 1;
        for (int i = 0; i < nf; i++) {
            ll pk = 1;
            for (int j = 0; j < e[i]; j++) pk *= factors[i].first;
            d *= pk;
        }

        if (d > 1 && has_order_60(d)) {
            total += d - 1;
            count++;
        }

        // Increment exponent vector (odometer)
        int carry = 1;
        for (int i = 0; i < nf && carry; i++) {
            e[i] += carry;
            if (e[i] > maxexp[i]) {
                e[i] = 0;
                carry = 1;
            } else {
                carry = 0;
            }
        }
        if (carry) break;
    }

    return total;
}

// --- Method 2: Brute force for small decks ---
ll solve_brute(int max_deck) {
    ll total = 0;
    for (int ds = 2; ds <= max_deck; ds += 2) {
        if (mult_order(2, ds + 1) == 60) {
            total += ds;
        }
    }
    return total;
}

int main() {
    // Method 1: Full solution
    ll ans1 = solve_divisor_enum();
    cout << "Divisor enumeration: " << ans1 << endl;

    // Method 2: Verify with small decks
    ll brute_small = solve_brute(10000);
    cout << "Brute force (decks <= 10000): " << brute_small << endl;

    // Cross-check: the brute force sum should match the enum sum
    // restricted to m <= 10001
    // (The full answer includes much larger m values)

    assert(ans1 == 3010983666182123972LL);
    cout << "\nAnswer: " << ans1 << endl;

    return 0;
}