Question: See Hahn Chapter Loops Problem 1 : The Prime Directive [ MG Also ] Filename: Function Declaration: function [ isItPrime ] = primeDirective ( numberToTest
See Hahn Chapter "Loops"
Problem : The Prime Directive MG Also
Filename:
Function Declaration:
function isItPrime primeDirectivenumberToTest A number is prime if it is not an exact multiple of any other number except itself
and ie if it has no factors except itself and The easiest plan of attack then is
as follows. Suppose P is the number to be tested. See if any numbers N can be
found that divide into without remainder. If there are none, is prime. Which
numbers should we try? Well, we can speed things up by restricting to odd
numbers, so we only have to try odd divisors N When do we stop testing? When
No we can stop a lot sooner. In fact, we can stop once reaches since if
there is a factor greater than there must be a corresponding one less than
which we would have found. And where do we start? Well, since will be a
factor of any P we should start at The structure plan is as follows
Input from the input parameter of the function
Initialize to
Find remainder when is divided by
While and repeat:
Increase by
Find when is divided by
If then
is prime
Else
is not prime
Stop
Return the correct value
Note that there may be no repeats
might be zero the first time.
Note also that there are two conditions under which the loop may stop.
Consequently, an if is required after completion of the loop to determine which
condition stopped it
Write the function. Then try it out on the following numbers: not prime
prime and prime Code to call your function
isNumberPrime roundrand;
primResult primeDirectiveisNumberPrime;
By using Matlab
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
