Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function result = primeCheck(x)
- %Create a persistent variable
- persistent myPersVar
- if mod(myPersVar, 2) == 1
- myPersVar = myPersVar +2;
- end
- %First verify that x is a positive integer
- %Zero and negatives are conditioned, as well as all non-integers
- %1 only non prime that is neither prime nor composite
- if x <= 0
- elseif mod(x, 1) ~= 0
- disp('Not a valid input, try a positive integer')
- elseif x== 1
- result = 'Non-Prime';
- end
- if x == 2
- result = 'Prime';
- elseif mod(x,2) == 0
- result = 'Non-Prime';
- elseif mod(x, myPersVar) == 0
- result = primeCheck(mod(x, myPersVar));
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement