Lucas-Lehmer test: Difference between revisions
Content added Content deleted
(Modula-3) |
|||
Line 231: | Line 231: | ||
{{works with|GHC|6.8.2}} |
{{works with|GHC|6.8.2}} |
||
<lang haskell>module Main |
|||
where |
|||
main = printMersennes $ take 45 $ filter lucasLehmer $ sieve [2..] |
|||
s mp 1 = 4 `mod` mp |
|||
s mp n = ((s mp $ n-1)^2-2) `mod` mp |
|||
lucasLehmer 2 = True |
|||
lucasLehmer p = s (2^p-1) (p-1) == 0 |
|||
printMersennes = mapM_ (\x -> putStrLn $ "M" ++ show x)</lang> |
|||
printMersennes (x:xs) = do putStrLn $ "M"++(show x) |
|||
printMersennes xs |
|||
It is pointed out on the [[Sieve of Eratosthenes]] page that the following "sieve" is inefficient. Nonetheless it takes very little time compared to the Lucas-Lehmer test itself. |
It is pointed out on the [[Sieve of Eratosthenes]] page that the following "sieve" is inefficient. Nonetheless it takes very little time compared to the Lucas-Lehmer test itself. |
||
<lang haskell>sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p > 0]</lang> |
|||
It takes about 30 minutes to get up to: |
It takes about 30 minutes to get up to: |
||
<pre> |
|||
M2 M3 M5 M7 M13 M17 M19 M31 M61 M89 M107 M127 M521 M607 M1279 M2203 M2281 M3217 M4253 M4423 M9689 M9941 M11213 |
|||
</pre> |
|||
=={{header|J}}== |
=={{header|J}}== |