Primes coding challenges

Practise primes with 14 coding challenges, from easy to hard. Every challenge runs in your browser with instant tests, an AI review of your code and community solutions to compare.

Easy · 4 Medium · 9 Hard · 1

Skills you'll practise: Primality testingSieve of EratosthenesSquare root boundDivisor pairsBrute-force searchDigit rotationDivisor sieveDivisor-sum sieveExponentiation by squaringModular arithmeticNumber truncationPair checks
  • Python · C++ · C#
  • Automated tests
  • AI code review
  • Community solutions
341
Challenges
241
Solutions shared
9
Languages

All challenges

Search by name or topic, or filter by difficulty.

14 challenges
Medium

Amicable Pair

List every amicable pair whose two numbers are both at most a limit.

You'll learn Divisor-sum sievePair checks
CS1High schoolIntermediateLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Medium

Prime Factorization

Break a number into its prime factors, listed from smallest to largest with repetition.

You'll learn Trial divisionSquare root bound
CS1High schoolIntermediateLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Easy

Count Primes in a Range

Count how many prime numbers lie between two bounds, both included.

You'll learn Sieve of EratosthenesRange counting
BeginnerCS1High schoolLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Medium

Goldbach Partition

Write an even number as the sum of two primes, using the smallest possible first prime.

You'll learn Primality testingSearching from the smallest candidate
CS1Dynamic-programmingHigh schoolIntermediateLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Hard

Non-Abundant Sums

Add up the numbers up to a limit that cannot be written as the sum of two abundant numbers.

You'll learn Divisor sievePair enumeration
AdvancedCompetitive-programmingNumber-theoryPrimesProject Euler stylemath
No solutions yet
Medium

Quadratic Primes

Find the quadratic n² + an + b that gives the longest run of primes from n = 0.

You'll learn Primality testingBrute-force search
Competitive-programmingIntermediateNumber-theoryPrimesProject Euler stylemath
No solutions yet
Medium

Nth Prime

Return the nth prime number, counting 2 as the first.

You'll learn Prime testingSieve of Eratosthenes
CS1High schoolIntermediateLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Easy

Twin Primes

List all twin prime pairs (p, p + 2) up to a limit.

You'll learn Sieve of EratosthenesPairing neighbours
BeginnerCS1High schoolLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Easy

Abundant, Perfect or Deficient

Classify a number as abundant, perfect or deficient by the sum of its proper divisors.

You'll learn Divisor pairsSquare root bound
BeginnerCS1High schoolLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Medium

Modular Exponentiation

Compute base to the power exponent, modulo a number, with fast exponentiation.

You'll learn Exponentiation by squaringModular arithmetic
CS1High schoolIntermediateLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Easy

Sum of Proper Divisors

Add up every divisor of n that is smaller than n.

You'll learn Divisor pairsSquare root bound
BeginnerCS1High schoolLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Medium

Euler's Totient

Count the integers from 1 to n that share no common factor with n.

You'll learn Prime factorizationProduct formula
CS1High schoolIntermediateLoopsNumber-theoryNumbersPrimesmath
No solutions yet
Medium

Truncatable Primes

List the primes up to a limit that stay prime as digits are removed from either end.

You'll learn Primality testingNumber truncation
Competitive-programmingIntermediateNumber-theoryPrimesProject Euler stylemath
No solutions yet
Medium

Circular Primes

Count the primes up to a limit whose every digit rotation is also prime.

You'll learn Primality testingDigit rotation
Competitive-programmingIntermediateNumber-theoryPrimesProject Euler stylemath
No solutions yet