home

Scientific Computing (Psychology 9040a)

Fall, 2021

Prime Number Test


Write a short script 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 MATLAB’s isprime() function) to test primeness. The purpose of this exercise is for you to write your own code from scratch.

Hint: you will probably want to use the MATLAB modulo function mod() to test whether the remainder is zero after dividing a number N by another number i.

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

sample solution