MAPLE - Simple procedure to test primality

  • Context: Maple 
  • Thread starter Thread starter dkotschessaa
  • Start date Start date
  • Tags Tags
    Maple Procedure Test
Click For Summary

Discussion Overview

The discussion revolves around creating a simple procedure in Maple to test for primality using an exhaustive method with the mod function. Participants are addressing a homework-related question about the implementation and functionality of the procedure.

Discussion Character

  • Homework-related, Technical explanation

Main Points Raised

  • One participant, Dave K, shares a procedure for testing primality and describes issues with its functionality, particularly regarding the loop and output.
  • Another participant questions whether the procedure should return values or print them, suggesting that the logic for handling non-prime numbers needs clarification.
  • A different participant proposes a solution involving the use of a "FoundDivisor" variable to manage the loop's logic and return a boolean value instead of printing directly.
  • Dave K indicates progress in understanding the problem after receiving feedback, but does not specify what was figured out.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the implementation details, as there are multiple suggestions for how to structure the procedure and handle outputs.

Contextual Notes

There are unresolved aspects regarding the desired output format (returning values vs. printing) and the handling of non-prime detection within the loop.

Who May Find This Useful

Individuals learning Maple programming, particularly those interested in algorithm implementation for mathematical concepts like primality testing.

dkotschessaa
Messages
1,063
Reaction score
763
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
 
Physics news on Phys.org
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.
 
I figured something else out, but thanks for your reply!
 

Similar threads

  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
2
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
905
Replies
8
Views
2K
  • · Replies 28 ·
Replies
28
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
6K