Psychology 9040a: Scientific Computing with MATLAB
Prime Number Test
Write a MATLAB script that determines whether the numbers 2 through N are prime numbers. To start, let N equal 100. 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 brute force method.
Hint: you will probably want to use the modulo operator to test
whether the remainder is zero after dividing a number n by another
number m. In MATLAB, modulo is achieved using the function mod(n,m)
.
So for example we can test whether the number 5 is prime by testing
whether mod(5,m)
equals 0 for m=2,3,4.