F# Returning a Boolean
First shot at Euler #3 in F#, and I would like to return a boolean more
elegantly than this mutable value.
// A number is prime if can only divide by itself and 1. Can only be odd.
let isPrime x =
if (x%2L = 0L) then
false
else
let mutable result = true
for i in 3L..x/2L do
if (x%i = 0L) then
result <- false
result
let a = isPrime(17L)
// True
printfn "%b" a
The L's are as I'm forcing the function to return bigints (there has to be
a better way too, but 1 step at a time)....
No comments:
Post a Comment