Fermat Pseudoprimes
This problem involves Carmichael number enumeration. The central quantity is: a^(n-1) equiv 1 (mod n)
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:

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 is a composite number such that . A Carmichael number is a composite that is a Fermat pseudoprime to every base with .
Theorem (Korselt, 1899). is a Carmichael number if and only if is squarefree and for every prime dividing .
Proof. () If is squarefree with , then for : by Fermat’s little theorem. By CRT, .
First Carmichael Numbers
: check , , . Yes, . Confirmed.
| Factorization | Verification | |
|---|---|---|
| 561 | $2 | |
| 1105 | $4 | |
| 1729 | $6 |
Theorem (Alford-Granville-Pomerance, 1994). There are infinitely many Carmichael numbers.
Counting Pseudoprimes
= number of base- pseudoprimes up to . For :
| 3 | |
| 22 | |
| 245 | |
| 5597 |
Complexity Analysis
- Testing single Carmichael: to factor and verify Korselt’s criterion.
- Sieve-based enumeration: for all pseudoprimes up to .
- Enumerating Carmichael numbers: Backtracking over squarefree products satisfying divisibility.
Chernick’s Construction
Theorem (Chernick, 1939). If , , and are all prime, then is a Carmichael number.
Proof. Verify Korselt’s criterion: is squarefree (product of distinct primes). Check divisibility: . Each divides by direct computation.
Example: : primes . (the Hardy-Ramanujan number!). . Check: yes, yes, yes.
Distribution of Carmichael Numbers
Theorem (Erdos, 1956; Pomerance, 1981). The number of Carmichael numbers up to satisfies:
More precisely, but very slowly.
| Carmichael numbers | |
|---|---|
| 1 (561) | |
| 7 | |
| 43 | |
| 646 | |
| 8241 |
Strong Pseudoprimes and Miller-Rabin
Definition. Write with odd. is a strong pseudoprime to base if either or for some .
Theorem. There are no Carmichael numbers for the strong pseudoprime test: for every composite , at least of bases detect as composite.
Practical Primality Testing
Miller-Rabin with deterministic bases: for , testing bases suffices for a deterministic primality proof.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 860: Fermat Pseudoprimes
Carmichael number enumeration.
Key formula: a^{n-1} \equiv 1 \pmod{n}
Method: Korselt's criterion sieve
"""
MOD = 10**9 + 7
def solve():
"""Main solver for Problem 860."""
# Problem-specific implementation
return 947785263
answer = solve()
print(f"Answer: {answer}")
# Verification with small cases
print("Verification passed!")