Difference between revisions of "Talk:99 questions/Solutions/31"
Jump to navigation
Jump to search
(New page: Does something like this deserves being put on the wiki ? <haskell> isPrime :: Int -> Bool isPrime n | n <= 1 = False isPrime n = isPrime' 2 n where isPrime' x n | x*x > n = True ...) |
|||
(2 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | Does something |
+ | Does something as simple but as dump as this should be on the wiki ? |
<haskell> |
<haskell> |
||
isPrime :: Int -> Bool |
isPrime :: Int -> Bool |
||
Line 7: | Line 7: | ||
| otherwise = (n `rem` x) /= 0 && isPrime' (x+1) n |
| otherwise = (n `rem` x) /= 0 && isPrime' (x+1) n |
||
</haskell> |
</haskell> |
||
+ | |||
+ | == And something as simple as this one... == |
||
+ | |||
+ | <haskell> |
||
+ | isPrime :: Int -> Bool |
||
+ | isPrime n = all (\x -> n `mod`x /= 0) [2..sqr] |
||
+ | where sqr = floor (sqrt (fromIntegral n :: Double)) |
||
+ | </haskell> |
||
+ | -- Assuming that a number is not a prime if he is divisible by any number between 2 and his sqrt |
Revision as of 11:15, 14 October 2013
Does something as simple but as dump as this should be on the wiki ?
isPrime :: Int -> Bool
isPrime n | n <= 1 = False
isPrime n = isPrime' 2 n
where isPrime' x n | x*x > n = True
| otherwise = (n `rem` x) /= 0 && isPrime' (x+1) n
And something as simple as this one...
isPrime :: Int -> Bool
isPrime n = all (\x -> n `mod`x /= 0) [2..sqr]
where sqr = floor (sqrt (fromIntegral n :: Double))
-- Assuming that a number is not a prime if he is divisible by any number between 2 and his sqrt