Given a natural number $n> 0$, define a function
IsPrime(n)that tells whether it is, or not, a prime number.
I tried do two functions:
HasDivider(x,y) | FALSE if y = 1
| TRUE if mod(x,y) = 0
| HasDivider(x, y-1) if mod(x,y) > 0
IsPrime(n) | TRUE if HasDivider(n, n/2) = FALSE
| FALSE if HasDivider(n, n/2) = TRUE
There is any error? or other way to do?
There are many of issues with the code sample you have provided, beyond the fact that it is mathematically unsound. I would encourage you to post this on StackOverflow for feedback concerning your use of Haskell.
Consider the value of
IsPrimeat $n=2$. Is it not true that $2 \equiv 0 \pmod{1}$? Or in Haskell,Hence, as written, your function will find
2to be a composite number.In case it proves helpful, the following summarizes a few of the issues with you Haskell sample.
HasDivider x y, notHasDivider(x,y)HasDivider a bnotHasDivider(a,b)Boolconstants areTrueandFalse, notTRUEandFALSE/performs floating point division. You want integer division, which usesdiv.remandmod. For you purposes,remshould be preferred, as it is generally faster.ifdoes not exist in the way you are trying to use it.Your (still inefficient) function might resemble the following in valid Haskell