Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Maple MAPLE - Simple procedure to test primality

  1. Jan 21, 2014 #1
    This is technically a "homework" type question so I just need a bit of help.

    I'm just learning maple and writing a very basic procedure. I'm testing primality exhaustively by using the mod function. (Yes, maple has a built in procedure for this, but this is just an exercise).

    ISPRIME := proc (n)
    local i;
    local s;
    if n < 2 then return false end if;
    if n = 2 then return true end if;
    for i from 2 to n do if n mod i = 0 then print (false) else print(true);
    end do;
    end proc;

    I kind of know why this isn't working but I don't know how to fix it. If I find an i such that n mod i is zero, then the number n is not prime - so that's all. Print (false) in that case.

    If it is not the case that n mod i = 0 then I want to continue testing all the different i's up to n. (really n-1) if I don't get anything, print (true). But it seems to not be looping the procedure for all the possible i's.

    Help/hints appreciated. Thanks.

    -Dave K
  2. jcsd
  3. Jan 23, 2014 #2
  4. Jan 27, 2014 #3
    Since this isn't a homework problem, I can just give you the answer.

    I would have your "IsPrime" function return true or false and have the calling program do the printing.

    You should set a "FoundDivisor" variable to false. Then inside the loop, if the Mod function returns zero, set FoundDivisor to true and "break" from the loop. If you drop through the loop without finding a divisor, "FoundDivisor" will still be false. So then just return not FoundDivisor.
  5. Jan 27, 2014 #4
    I figured something else out, but thanks for your reply!
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Similar Threads for MAPLE Simple procedure
Maple Computing Numerical Integrals with Maple
Maple ODE System solution Maple
LaTeX \vec command