Using ML SML of New Jersey Write a funciton isPrime of type

Using ML: (SML of New Jersey)

Write a funciton isPrime of type int -> bool that returns true if
and only if its integer parameter is a prime number. Your function need not behave well
if the parameter is negative.

Solution

(* Returns whether n is prime. Requires: n is a positive integer. *) fun isPrime(n: int): bool = let fun noDivisorsAbove(m: int) = if n mod m = 0 then false else if m*m >= n then true else noDivisorsAbove(m+1) in noDivisorsAbove(2) end
Using ML: (SML of New Jersey) Write a funciton isPrime of type int -> bool that returns true if and only if its integer parameter is a prime number. Your fun

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site