Skip to content

Opdracht 37 Priemgetallen

Learning goals:

  • Create and use functions.
  • Use the modulo operator.

Task:

A very particular kind of numbers is a prime number. Prime numbers have a specific quality: they cannot be formed by doing \(m *n\) (except 1 times itself). In other words, 18 is not a prime number because it can be formed by \(2 * 9\), however 9 is a prime number because it can only be formed by \(1 * 9\).

Using a boolean function is_prime(n), create a program called prime_numbers.py that takes a starting number and an ending number and prints all prime numbers in that range. Make sure the starting and ending number are both positive and re-prompt the user each time they provide an invalid input.

Example output:

Enter starting #: 1
Enter ending #: 100
Prime #s between 1 and 100:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Enter starting #: 200
Enter ending #: 300
Prime #s between 200 and 300:
211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293
Enter starting #: -9
    Invalid!
Enter starting #: 30
Enter ending #: 60
Prime #s between 30 and 60:
31 37 41 43 47 53 59

Learn more: