Skip to content

Instantly share code, notes, and snippets.

@freemo
Last active September 20, 2019 17:00
Show Gist options
  • Save freemo/8628565 to your computer and use it in GitHub Desktop.
Save freemo/8628565 to your computer and use it in GitHub Desktop.
Primality Algoritm, Multiple languages
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..]]
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