Find the Prime Numbers
Python
30 pts
Find the Prime Numbers
A prime number is a whole number greater than 1 that can only be divided evenly by 1 and itself (2, 3, 5, 7, 11 …).
Find all prime numbers from 2 to 30 using nested for loops:
- Create an empty list called
primes. - The outer loop goes through the numbers 2 to 30.
- The inner loop tries every divisor from 2 up to (but not including) the number. If
number % divisor == 0, the number is not prime:break. - Use
elseon the inner loop: if no divisor was found, append the number toprimes. - Print the list and how many primes you found.
Expected output:
Primes: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29] Count: 10
Checklist
- Check only the numbers from 2 to 30 and collect the results in
primes. - Use an inner loop with
breakandelseso that only prime numbers are appended. - Print the list and the count.
🔑 Solution
If you view the solution before passing, you earn half points for this exercise. You still need to type it and press Check to finish.
How it works, step by step
Point at a step to highlight its lines. Type the solution yourself and press Check answer to finish.
