Exercise: Primeness (Function)¶
Modify the prime number detection program from here or here as follows.
Push the prime number checking algorithm into a function. (
is_prime()
is a likely name for that function.)The function takes one parameter: the number to check for primeness.
The function returns whether the number-to-check is prime (a boolean value)
The function does not have any side effects, like printing on the console, or use
sys.exit()
to terminate the process. All that must be done by the caller.
The rest of the program (taking the number from the commandline, or
using input()
to prompt the user for the number) remains
intact. The only change is that the new function is called with the
number as parameter, rather than implement the algorithm in top level
code.