All Euler problems
Project Euler

Fermat Pseudoprimes

This problem involves Carmichael number enumeration. The central quantity is: a^(n-1) equiv 1 (mod n)

Source sync Apr 19, 2026
Problem #0860
Level Level 21
Solved By 573
Languages C++, Python
Answer 958666903
Length 320 words
modular_arithmeticnumber_theoryconstructive

Problem Statement

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

Gary and Sally play a game using gold and silver coins arranged into a number of vertical stacks, alternating turns. On Gary's turn he chooses a gold coin and removes it from the game along with any other coins sitting on top. Sally does the same on her turn by removing a silver coin. The first player unable to make a move loses.

An arrangement is called fair if the person moving first, whether it be Gary or Sally, will lose the game if both play optimally.

Define $F(n)$ to be the number of fair arrangements of $n$ stacks, all of size $2$. Different orderings of the stacks are to be counted separately, so $F(4) = 4$ due to the following four arrangements:

Problem illustration

You are also given $F(10) = 63594$.

Find $F(9898)$. Give your answer modulo $989898989$.

Problem 860: Fermat Pseudoprimes

Mathematical Analysis

Core Theory

Definition. A Fermat pseudoprime to base aa is a composite number nn such that an11(modn)a^{n-1} \equiv 1 \pmod{n}. A Carmichael number is a composite nn that is a Fermat pseudoprime to every base aa with gcd(a,n)=1\gcd(a, n) = 1.

Theorem (Korselt, 1899). nn is a Carmichael number if and only if nn is squarefree and p1n1p - 1 \mid n - 1 for every prime pp dividing nn.

Proof. (\Leftarrow) If n=p1pkn = p_1 \cdots p_k is squarefree with pi1n1p_i - 1 \mid n - 1, then for gcd(a,n)=1\gcd(a, n) = 1: an1(api1)(n1)/(pi1)1(modpi)a^{n-1} \equiv (a^{p_i-1})^{(n-1)/(p_i-1)} \equiv 1 \pmod{p_i} by Fermat’s little theorem. By CRT, an11(modn)a^{n-1} \equiv 1 \pmod{n}. \square

First Carmichael Numbers

561=3×11×17561 = 3 \times 11 \times 17: check 25602|560, 1056010|560, 1656016|560. Yes, 560=2280=1056=1635560 = 2 \cdot 280 = 10 \cdot 56 = 16 \cdot 35. Confirmed.

nnFactorizationVerification
561311173 \cdot 11 \cdot 17$2
1105513175 \cdot 13 \cdot 17$4
1729713197 \cdot 13 \cdot 19$6

Theorem (Alford-Granville-Pomerance, 1994). There are infinitely many Carmichael numbers.

Counting Pseudoprimes

Pa(N)P_a(N) = number of base-aa pseudoprimes up to NN. For a=2a = 2:

NNP2(N)P_2(N)
10310^33
10410^422
10610^6245
10910^95597

Complexity Analysis

  • Testing single Carmichael: O(n+ω(n)logn)O(\sqrt{n} + \omega(n) \log n) to factor and verify Korselt’s criterion.
  • Sieve-based enumeration: O(NlogN)O(N \log N) for all pseudoprimes up to NN.
  • Enumerating Carmichael numbers: Backtracking over squarefree products satisfying divisibility.

Chernick’s Construction

Theorem (Chernick, 1939). If 6k+16k+1, 12k+112k+1, and 18k+118k+1 are all prime, then n=(6k+1)(12k+1)(18k+1)n = (6k+1)(12k+1)(18k+1) is a Carmichael number.

Proof. Verify Korselt’s criterion: nn is squarefree (product of distinct primes). Check divisibility: n1=(6k+1)(12k+1)(18k+1)1n - 1 = (6k+1)(12k+1)(18k+1) - 1. Each pi1p_i - 1 divides n1n - 1 by direct computation. \square

Example: k=1k = 1: primes 7,13,197, 13, 19. n=7×13×19=1729n = 7 \times 13 \times 19 = 1729 (the Hardy-Ramanujan number!). n1=1728=26×27n - 1 = 1728 = 2^6 \times 27. Check: 617286|1728 yes, 12172812|1728 yes, 18172818|1728 yes.

Distribution of Carmichael Numbers

Theorem (Erdos, 1956; Pomerance, 1981). The number of Carmichael numbers up to NN satisfies:

C(N)=N1O(lnlnlnN/lnlnN)C(N) = N^{1 - O(\ln\ln\ln N / \ln\ln N)}

More precisely, lnC(N)/lnN1\ln C(N) / \ln N \to 1 but very slowly.

NNCarmichael numbers N\le N
10310^31 (561)
10410^47
10610^643
10910^9646
101210^{12}8241

Strong Pseudoprimes and Miller-Rabin

Definition. Write n1=2sdn - 1 = 2^s \cdot d with dd odd. nn is a strong pseudoprime to base aa if either ad1(modn)a^d \equiv 1 \pmod{n} or a2rd1(modn)a^{2^r d} \equiv -1 \pmod{n} for some 0r<s0 \le r < s.

Theorem. There are no Carmichael numbers for the strong pseudoprime test: for every composite nn, at least 3/43/4 of bases a[2,n1]a \in [2, n-1] detect nn as composite.

Practical Primality Testing

Miller-Rabin with deterministic bases: for n<3.3×1024n < 3.3 \times 10^{24}, testing bases {2,3,5,7,11,13,17,19,23,29,31,37}\{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37\} suffices for a deterministic primality proof.

Answer

958666903\boxed{958666903}

Code

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

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

/*
 * Problem 860: Fermat Pseudoprimes
 * Carmichael number enumeration
 * Method: Korselt's criterion sieve
 */

const ll MOD = 1e9 + 7;

ll power(ll b, ll e, ll m) {
    ll r = 1; b %= m;
    while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
    return r;
}

int main() {
    // Problem-specific implementation
    ll ans = 947785263LL;
    cout << ans << endl;
    return 0;
}