Last active
September 20, 2019 17:00
-
-
Save freemo/8628565 to your computer and use it in GitHub Desktop.
Primality Algoritm, Multiple languages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
prime :: Int -> Bool | |
prime x | |
| x == 1 = False | |
| x == 2 = True | |
| x == 3 = True | |
| x `mod` 2 == 0 = False | |
| x `mod` 3 == 0 = False | |
| otherwise = all (\y -> x `mod` y /= 0) $ dividends x | |
where | |
dividends z = | |
takeWhile (<= (floor . sqrt . fromIntegral $ z)) | |
$ concatMap (\x -> [x, x+2]) [6 * x - 1 | x <- [1..]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isPrime(number) { | |
if ((number == 2) || (number == 3)) | |
return true; | |
if ((number % 2 === 0) || (number % 3 === 0)) | |
return false; | |
var maxDivisor = Math.sqrt(number); | |
var dividendIndex = 1; | |
var dividend = 5; | |
while (dividend < maxDivisor) { | |
if (number % dividend === 0) | |
return false; | |
dividend = dividend + 2; | |
if (dividend > maxDivisor) | |
break; | |
if (number % dividend === 0) | |
return false; | |
dividendIndex++; | |
dividend = 6 * dividendIndex - 1; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment