Carmichael 3 strong pseudoprimes: Difference between revisions

→‎{{header|D}}: added Haskell
m (→‎{{header|REXX}}: optimized the '''IF''' statements by unrolling them. -- ~~~~)
(→‎{{header|D}}: added Haskell)
Line 177:
61 x 241 x 421
61 x 3361 x 4021</pre>
 
=={{header|Haskell}}==
 
{{trans|Ruby}}
{{libheader|primes}}
{{Works with|GHC|7.4.1}}
{{Works with|primes|0.2.1.0}}
 
<lang haskell>#!/usr/bin/runhaskell
 
import Data.Numbers.Primes
import Data.Maybe
 
concatEach = flip concatMap
eachMaybe = flip mapMaybe
 
carmichaels =
(takeWhile (<= 61) primes) `concatEach` (\p ->
[2..(p-1)] `concatEach` (\h3 ->
let g = h3 + p
in [1..(g-1)] `eachMaybe` (\d ->
if (g * (p - 1)) `mod` d == 0 && (-1 * p * p) `mod` h3 == d `mod` h3 then
let q = 1 + (((p - 1) * g) `div` d)
in if isPrime q then
let r = 1 + ((p * q) `div` h3)
in if isPrime r && (q * r) `mod` (p - 1) == 1 then
Just (p, q, r)
else
Nothing
else
Nothing
else
Nothing)))
 
main = putStr $ unlines $ map show carmichaels</lang>
 
{{out}}
 
<pre>
(3,11,17)
(5,29,73)
(5,17,29)
(5,13,17)
(7,19,67)
(7,31,73)
(7,13,31)
(7,23,41)
(7,73,103)
(7,13,19)
(13,61,397)
(13,37,241)
(13,97,421)
(13,37,97)
(13,37,61)
(17,41,233)
(17,353,1201)
(19,43,409)
(19,199,271)
(23,199,353)
(29,113,1093)
(29,197,953)
(31,991,15361)
(31,61,631)
(31,151,1171)
(31,61,271)
(31,61,211)
(31,271,601)
(31,181,331)
(37,109,2017)
(37,73,541)
(37,613,1621)
(37,73,181)
(37,73,109)
(41,1721,35281)
(41,881,12041)
(41,101,461)
(41,241,761)
(41,241,521)
(41,73,137)
(41,61,101)
(43,631,13567)
(43,271,5827)
(43,127,2731)
(43,127,1093)
(43,211,757)
(43,631,1597)
(43,127,211)
(43,211,337)
(43,433,643)
(43,547,673)
(43,3361,3907)
(47,3359,6073)
(47,1151,1933)
(47,3727,5153)
(53,157,2081)
(53,79,599)
(53,157,521)
(59,1451,2089)
(61,421,12841)
(61,181,5521)
(61,1301,19841)
(61,277,2113)
(61,181,1381)
(61,541,3001)
(61,661,2521)
(61,271,571)
(61,241,421)
(61,3361,4021)
</pre>
 
=={{header|Mathematica}}==
Anonymous user