Test every divisor from 2 to N – 1, keep the answer in an isPrime flag, and stop early with break
Checking whether a number is prime is one of the best first problems for loops: it combines a loop counter, the modulus operator and a flag variable. In this lesson we build the prime number flowchart step by step and dry-run it on 11 and 6.
At the end we make it faster with break: as soon as one divisor is found, the answer is known.
What is a prime number?
A prime number is a whole number greater than 1 with exactly two divisors: 1 and itself. So 2, 3, 5, 7, 11 and 13 are prime; 6 is not (it is divisible by 2 and 3), and 1 is not prime either (it has only one divisor).

Every number is divisible by 1, so the real test is: does any number from 2 to N – 1 divide N? If none does, N is prime.
The test: number % LC == 0
The loop counter (LC) goes from 2 while LC < number. On every step we ask whether LC divides the number exactly, using the remainder:
number % LC == 0 -> LC divides the number (no remainder)
For 11: 11 % 2 = 1, 11 % 3 = 2 (3 x 3 = 9, remainder 2) and so on up to 10. No remainder is ever 0, so 11 is prime.
The prime number flowchart, step by step
- Start, read number, set LC = 2 and isPrime = 1 (we assume prime until a divisor shows up).
- LC < number? If no, the loop is over: go to step 5.
- number % LC == 0? If yes, a divisor is found: isPrime = 0.
- LC = LC + 1, and back to step 2.
- isPrime == 1? Yes: print number is prime. No: print number is not prime. Then End.

Why a flag and not a print inside the loop? A ‘not prime’ print inside the loop would print once for every divisor. On the board that box is crossed out: the flag remembers the answer and the program prints it once, after the loop.
Dry run: 11 and 6
| number | LC values tested | divisor found? | isPrime | result | |—|—|—|—|—| | 11 | 2, 3, 4 … 10 | none | 1 | prime | | 6 | 2, 3, 4, 5 | yes, at LC = 2 | 0 | not prime |
Stop early with break
For 6 the answer is clear at LC = 2: 6 % 2 == 0. Testing 3, 4 and 5 afterwards changes nothing; it only costs time. So when a divisor is found, set isPrime = 0 and break out of the loop.
LC = 2
isPrime = 1
while LC < number
if number % LC == 0
isPrime = 0
break
LC = LC + 1
if isPrime == 1
print "number is prime"
else
print "number is not prime"
An even faster version only tests LC up to the square root of the number (the slide in the video mentions it): if N has a divisor other than 1 and N, it has one no bigger than its square root.
Video chapters
- 0:00 Prime or not: the problem
- 0:23 What is a prime number?
- 0:56 Dry run: 6 and 11
- 1:37 The loop: 2 to N – 1, check divisibility
- 2:03 Drawing the flowchart
- 4:31 Dry run on the flowchart: 11
- 6:02 Dry run: 6 (not prime)
- 7:18 Advanced: break the loop early
- 8:12 Wrap-up
Watch the short versions
Short on time? These Shorts from this lesson each make one point in under a minute.
- What Is a Prime Number? (The Quick Test) (19 s Short, coming soon, from 0:34 in the full video)
- Is 11 Prime? Check 2 to 10 (23 s Short, coming soon, from 1:10 in the full video)
- Prime Check Trick: Stop the Loop with Break (38 s Short, coming soon, from 7:33 in the full video)
Key takeaways
- A prime has exactly two divisors, 1 and itself; 1 is not prime.
- Test divisors from 2 to N – 1 with number % LC == 0.
- Keep the answer in a flag (isPrime) and print once, after the loop.
- break stops the loop at the first divisor and saves the remaining iterations.
- Testing only up to the square root of N gives the same answer faster.
Frequently asked questions
Why does the loop start at 2 and not 1?
Every number is divisible by 1, so starting at 1 would mark every number as not prime. The test only needs the numbers between 1 and N.
Is 1 a prime number?
No. A prime needs exactly two different divisors; 1 has only one. In this flowchart the loop never runs for 1, so add a check for numbers below 2.
What does break do here?
It leaves the loop as soon as a divisor is found, because the answer (not prime) can no longer change.
Further reading
- Trial division (Wikipedia): testing divisors up to the square root of n
- Is 1 prime? (Brilliant): why 1 is not a prime number
Practice: change the loop to stop at the square root of the number. How many tests does 97 need now?
Watch the video, subscribe to the channel and follow the series in order.
