This is homework. I already turned it in. I just don't know why this function doesn't work. I just want to learn what I missed. Thanks so much. This is the problem:
Write a function myprime that takes n, a positive integer,as an input and returns true if n is prime or returns false otherwise. Do not use the isprime or primes or factor built-‐in functions.
Here is my code:
function result = myprime(n)%%initially set output flag to true
result = true;%%iterate over all positive integers 2,3,...,n-1
%%if n is not divisible by any of these factors....it is prime
if (n == 1) result = 'false'; elseif (n == 2) result = 'true'; else for i=2:n-1, if (mod(n,i)==0) result = 'false'; end end end%%return "true" or "false" instead of 1 or 0
if (result) result = 'true'; else result = 'false'; end
Best Answer