The sieve of Sundaram: Difference between revisions

Undo revision 340192 by Hout (talk)
(→‎{{header|Haskell}}: Dropped use of Data.Set)
(Undo revision 340192 by Hout (talk))
Line 284:
<lang haskell>import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import qualified Data.Set as S
import Text.Printf (printf)
 
Line 289 ⟶ 290:
 
sundaram :: Integral a => a -> [a]
sundaram n = succ . (2 *) <$> gaps [1 ..] excluded
[ succ (2 * x)
| x <- [1 .. m],
x `S.notMember` excluded
]
where
m = div (pred n) 2
excluded =
[ 2 * i * j + i + jS.fromList
|[ let2 fm* =i fromIntegral* m,j + i + j
i| <-let [1fm .. floor (sqrt (fm= /fromIntegral 2))]m,
let fi =i fromIntegral<- i[1 .. floor (sqrt (fm / 2))],
j <- [i .. floor ((fm -let fi) /= succfromIntegral (2 * fi))]i,
j <- [i .. floor ((fm - fi) / succ (2 * fi))]
]
]
 
nSundaramPrimes ::
Line 304 ⟶ 310:
nSundaramPrimes n =
sundaram $ floor $ (2.4 * n * log n) / 2
 
 
 
Line 312 ⟶ 319:
(putStrLn . table " " . chunksOf 10) $
show <$> nSundaramPrimes 100
 
 
------------------------- GENERIC ------------------------
 
gaps _ [] = []
gaps (x : xs) yys@(y : ys)
| x >= y = gaps xs ys
| otherwise = x : gaps xs yys
 
table :: String -> [[String]] -> String
9,655

edits