# MAPLE - Simple procedure to test primality

• Maple
• dkotschessaa
In summary, the conversation discusses a question about a procedure in Maple for testing primality. The person is having trouble with the loop and needs help fixing it. They also mention wanting the function to return true or false, not print it. The solution involves setting a variable and using the "break" function.
dkotschessaa
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

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.

Hello Dave K,

Thank you for reaching out for help with your maple procedure. It seems like you have a good understanding of the concept of testing primality, but there are a few errors in your code that may be causing it not to work as intended.

Firstly, in your for loop, you are starting at i=2 and going up to n, which means that you are only checking if n is divisible by 2 and n itself. This is not enough to determine if a number is prime. You need to check for divisibility by all numbers from 2 to n-1.

Secondly, your if statement is not correctly structured. You have it set up to print false if n mod i is 0, but you also need to have an else statement to print true if n mod i is not 0. As it is currently written, your code will only print false if n is not divisible by 2.

Here is a revised version of your code that should work correctly:

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

I hope this helps you with your assignment. If you have any further questions, please don't hesitate to ask. Good luck with your studies!

## 1. What is a simple procedure to test primality in MAPLE?

The simplest procedure to test primality in MAPLE is to use the isprime() function. This function takes in a single argument (an integer) and returns a boolean value of true if the integer is prime, and false if it is not.

## 2. Can MAPLE handle large prime numbers?

Yes, MAPLE is capable of handling extremely large prime numbers. The isprime() function can handle numbers up to 263-1, which is approximately 9.2 quadrillion. For larger numbers, the nextprime() function can be used to find the next prime number after a given integer.

## 3. How accurate is the primality test in MAPLE?

The primality test in MAPLE is extremely accurate. It uses the Miller-Rabin primality test, which has been proven to correctly determine primality for all integers up to 264. For larger numbers, the test is probabilistic, but the chances of a false positive are extremely low.

## 4. Are there any built-in functions in MAPLE for factoring large numbers?

Yes, MAPLE has several built-in functions for factoring large numbers. The factor() function can be used to find the prime factors of a given integer, while the ifactor() function can be used to find the prime factors along with their multiplicities.

## 5. Can MAPLE be used for other types of number theory calculations?

Yes, MAPLE has a variety of built-in functions for number theory calculations. These include functions for computing greatest common divisors, modular arithmetic, and solving Diophantine equations. MAPLE also has a wide range of functions for working with prime numbers, including generating prime numbers and testing for different types of prime numbers.

• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
• Programming and Computer Science
Replies
22
Views
1K
• Precalculus Mathematics Homework Help
Replies
8
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
• MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
• Calculus and Beyond Homework Help
Replies
3
Views
140
• Thermodynamics
Replies
3
Views
1K