home

Scientific Computing (Psychology 9040a)

Fall, 2020

Prime Number Test


Write a short program that determines whether a number N is a prime number.

Remember, a prime number is a natural number greater than 1 that is only divisible with zero remainder by the number 1, and itself. The first few prime numbers are 2,3,5,7,11,13,17,19,23,29, …

For now you don’t have to implement a fancy algorithm for testing primeness (e.g. Sieve of Eratosthenes). For now, it’s ok to implement a simple, brute force method.

Do not use a function you find on the internet or a built-in function (such as in SymPy) to test primeness. The purpose of this exercise is for you to write your own code.

Hint: you will probably want to use the modulo operator (which is the symbol % in Python) to test whether the remainder is zero after dividing a number N by another number m.

Your program should run like so:

Enter a number: 7
The number 7 is prime

and:

Enter a number: 12
The number 12 is not prime

A sample solution: primes_sol