A Prime Test That Scales

Hard data structure and algorithm practice puzzle.

Trial division is hopeless for the huge primes cryptography needs. Miller-Rabin tests a number without factoring it. Write n - 1 as d times 2 to the r with d odd. For a base a, compute a to the d modulo n. The base passes if that is 1 or n - 1, or if squaring it up to r - 1 more times ever gives n - 1. A prime passes for every base. Using the bases 2, 3, 5 and 7 gives the right answer for every n here. Return true if n is prime.

Solve it three ways: snap the jigsaw pieces together, fill in the blanks, or type it from scratch against the tests. This one is free to play, with no account.

Every line of the solution comes with a note explaining why it is there, and a step by step replay shows the code running. At level 3, where you type it yourself, you can ask for an AI mock interview on this problem that ends with a hiring manager's scorecard.

Then grow it into a real project with the Software Engineer button: this puzzle becomes the Reading Permission Mask, a pipeline you can run and download. See every Software Engineer project.

More Math puzzles

All 27 Math practice problems

Browse all 676 puzzles