Long primes: Difference between revisions

m
→‎{{header|Haskell}}: Applied Hlint, Ormolu. Specified an import.
(Added Delphi reference to Pascal code)
m (→‎{{header|Haskell}}: Applied Hlint, Ormolu. Specified an import.)
Line 832:
</pre>
=={{header|Haskell}}==
<lang Haskell>import Data.List (elemIndex)
import Data.List
 
longPrimesUpTo :: Int -> [Int]
longPrimesUpTo n = filter isLongPrime $ takeWhile (<n) primes
filter isLongPrime n = found$
where sieve (p:xs) = p : sieve [x | x <- xs, x `mod` p /= 0]
takeWhile (< n) primes = sieve [2..]
where
isLongPrime n = found
sieve (p : xs) = p where: cyclessieve =[x take| n (iterate (\yx <-> 10xs, * yx `mod` n)p 1)/= 0]
primes = sieve [2 ..]
index = findIndex (== head cycles) $ tail cycles
isLongPrime n = found
found = case index of (Just i) -> n - i == 2
where
_ -> False
cycles = take n (iterate ((`mod` n) . (10 *)) 1)
index = findIndexelemIndex (== head cycles) $ tail cycles
found = case index of
found = case index of (Just i) -> n - i == 2
_ -> False
 
display :: Int -> IO ()
display n = if n <= 64000 then do
if n <= 64000
putStrLn (show n ++ " is " ++ show (length $ longPrimesUpTo n))
then do
display (n * 2)
else pure ()putStrLn
( show n <> " is "
putStrLn (show n ++ " is " ++<> show (length $ longPrimesUpTo n))
)
display (n * 2)
else pure ()
 
main :: IO ()
main = do
let fiveHundred = longPrimesUpTo 500
putStrLn
putStrLn ( "The long primes up to 35 are:\n" ++ show fiveHundred ++ "\n")
putStrLn ("500 is " ++ show (length fiveHundred))
<> show fiveHundred
display 1000
<> "\n"
</lang>
)
putStrLn ("500 is " ++<> show (length fiveHundred))
display 1000</lang>
{{out}}
<pre>The long primes up to 35 are:
<pre>
The long primes up to 35 are:
[7,17,19,23,29,47,59,61,97,109,113,131,149,167,179,181,193,223,229,233,257,263,269,313,337,367,379,383,389,419,433,461,487,491,499]
 
Line 870 ⟶ 881:
16000 is 716
32000 is 1300
64000 is 2430</pre>
</pre>
 
=={{header|J}}==
9,655

edits