Algorithm Atlas · worked example
See the algorithm, step by step
Write n-1 as d times a power of two and test whether modular powers witness compositeness.
This call is starting. Local values appear as they are initialized.
Data & variables
Changed Source indexExample input
n=15Follow the code. Data appears here when it enters scope.
Call stack · 1 frame
maincall 1
Rust source
Line 48fn is_prime(n: u64) -> bool {
fn power(mut a: u64, mut d: u64, n: u64) -> u64 {
let mut result = 1;
while d > 0 {
if d & 1 == 1 {
result = (result as u128 * a as u128 % n as u128) as u64;
}
a = (a as u128 * a as u128 % n as u128) as u64;
d >>= 1;
}
result
}
if n < 2 {
return false;
}
for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] {
if n % p == 0 {
return n == p;
}
}
let shifts = (n - 1).trailing_zeros();
let d = (n - 1) >> shifts;
// These seven bases cover unsigned 64-bit integers; u128 avoids overflow.
for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022] {
if a % n == 0 {
continue;
}
let mut x = power(a % n, d, n);
if x == 1 || x == n - 1 {
continue;
}
let mut witness = true;
for _ in 1..shifts {
x = (x as u128 * x as u128 % n as u128) as u64;
if x == n - 1 {
witness = false;
break;
}
}
if witness {
return false;
}
}
true
}
fn main() {
let result = is_prime(15).to_string();
println!("{result}");
}
Try your own version
Change the independent fixture `n=15` in a legal way, then compare the result with your prediction. Explain how the same invariant applies to the separate visible challenge `n=2147483647`.
Open personal copy in Playground ↗Start with this exact source and input, change a value, then use Run or Debug locally. This replay stays attached to the original example.
Recordings use a native Rust target. Playground runs on wasm32-wasip1; target-sized types such as usize can have different limits.

Understand it
The tested number is composite. The worked program demonstrates this invariant on an independent fixture, so you can study the implementation before predicting the challenge result.
For teaching input n=15, the worked program returns false. Its functions are complete and use only std. Adapt the same state transitions to the visible challenge input n=2147483647; predict that result before opening the answer reveal. The precise contract is: Parse unsigned n and return whether it is prime. The Rust example uses deterministic unsigned-64-bit witnesses; zero and one are not prime.
Return canonical output: lowercase booleans, decimal integers without leading zeros, comma-separated values and semicolon-separated groups, with no extra whitespace or final newline. Whole medians use integer form and half medians use .5. Textual sentinels such as none and # retain their stated meaning. String and character-grid fixtures use ASCII unless the lesson explicitly specifies Unicode scalar values.
Solve the exact task, not just the visible example: Parse unsigned n and return whether it is prime. The Rust example uses deterministic unsigned-64-bit witnesses; zero and one are not prime. On input n=2147483647, predict the output before opening the answer reveal. The revealed output is a test oracle for this case, while private checks exercise other legal cases. Start by stating the input grammar and output format in your own words. Parse the complete input, including surrounding whitespace, before relying on any convenient positions in the example.
Build the solution around one maintained claim: Write n-1 as d times a power of two and test whether modular powers witness compositeness. Decide what state represents that claim, how it is initialized, what a single update does, and when the algorithm stops. An arithmetic or bitwise transformation relies on an identity. Write that identity and the domain in which it holds. Trace the visible input by hand and record at least two intermediate states, not only the final answer. If the trace cannot justify your predicted output, there is a gap between the idea and the implementation.
Check both correctness and cost. The intended bound is Seven deterministic u64 bases; O(log n) modular multiplications per base using u128 products; explain which operation dominates it and how many times the input can trigger that operation. Test zero, one, powers of two, maximum input size, and overflow or underflow around intermediate products. Add one small case that should succeed, one boundary case, and one case chosen to challenge the invariant. Compare your hand result with the program's result, then use a failing case to fix the earliest incorrect state transition rather than patching the final output.
The pattern
Write n-1 as d times a power of two and test whether modular powers witness compositeness.
When to use it: fast primality for large machine integers
Complexity: Seven deterministic u64 bases; O(log n) modular multiplications per base using u128 products
Input: n=2147483647
Read the code
fn is_prime(n: u64) -> bool {
fn power(mut a: u64, mut d: u64, n: u64) -> u64 {
let mut result = 1;
while d > 0 {
if d & 1 == 1 {
result = (result as u128 * a as u128 % n as u128) as u64;
}
a = (a as u128 * a as u128 % n as u128) as u64;
d >>= 1;
}
result
}
if n < 2 {
return false;
}
for p in [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37] {
if n % p == 0 {
return n == p;
}
}
let shifts = (n - 1).trailing_zeros();
let d = (n - 1) >> shifts;
// These seven bases cover unsigned 64-bit integers; u128 avoids overflow.
for a in [2, 325, 9375, 28178, 450775, 9780504, 1795265022] {
if a % n == 0 {
continue;
}
let mut x = power(a % n, d, n);
if x == 1 || x == n - 1 {
continue;
}
let mut witness = true;
for _ in 1..shifts {
x = (x as u128 * x as u128 % n as u128) as u64;
if x == n - 1 {
witness = false;
break;
}
}
if witness {
return false;
}
}
true
}
fn main() {
let result = is_prime(15).to_string();
println!("{result}");
}
Complete std-only miller-rabin program for its typed teaching fixture. Parsing and output construction are separate from the core algorithm.
Practice starter
pub fn solve(input: &str) -> String {
// Parse unsigned n and return whether it is prime. The Rust example uses deterministic unsigned-64-bit witnesses; zero and one are not prime.
// Trim surrounding whitespace; implement parsing and the algorithm.
let _ = input;
todo!("implement miller-rabin")
}
Try it
Parse unsigned n and return whether it is prime. The Rust example uses deterministic unsigned-64-bit witnesses; zero and one are not prime.
What success looks like: The private harness prints PASS miller-rabin. It checks at least five distinct semantic inputs plus surrounding-whitespace variants. Explain complexity separately; this harness does not time the algorithm.
Check your understanding
Boundary check for miller-rabin: with input n=561, what result should solve return?
Reveal the answer
falsetrue- The valid input necessarily panics
For this input the result is false. The tested number is composite. Compare this independent fixture with the worked trace: For n=2147483647, the deterministic 64-bit base set passes every strong test, reporting true. Composite 561 is rejected. Decompose n-1 into odd d times a power of two, square residues, and use u128 modular products to avoid u64 overflow.
Go deeper
Common misconception: “The tested number is definitely prime after one random base”
The tested number is composite. The independent teaching fixture demonstrates this rule; predict the separate visible challenge before revealing its answer.
- Pattern-specific invariant: The tested number is composite. This is the property the named implementation maintains; explain which comparison, state update or recursive return in the worked program establishes it.
- Independent typed fixture: The worked call uses
n=15and computesfalse. Keep parsing separate, then identify the core state before and after this call. - Different legal case: For
n=561, the independently calculated result isfalse. The complexity discussion uses Seven deterministic u64 bases; O(log n) modular multiplications per base using u128 products. Verify the state invariant before reasoning about the operation count.
Transfer challenge: Change the independent fixture n=15 in a legal way, then compare the result with your prediction. Explain how the same invariant applies to the separate visible challenge n=2147483647.
Connections
- Miller–Rabin Primality: When to Use It: fast primality for large machine integers
Reveal the Algorithm Atlas answer
true
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Challenge: Miller–Rabin Primality (courseId "algorithms", lessonId "algorithm.miller-rabin.challenge") Link: https://crabrix.com/learn/algorithms/algorithm.miller-rabin.challenge Goal: The tested number is composite. Parse unsigned n and return whether it is prime. The Rust example uses deterministic unsigned-64-bit witnesses; zero and one are not prime. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=algorithms/algorithm.miller-rabin.challenge) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.