1. Shu Zong Chen
  2. euler-haskell

Commits

Shu Zong Chen  committed f2b60a3

Added helper modules to handle prime stuff

  • Participants
  • Parent commits ea7a5d8
  • Branches default

Comments (0)

Files changed (1)

File problems/Helpers/Primes.hs

View file
+module Helpers.Primes
+( prime_sieve
+,
+) where
+
+sieve':: Integer -> [Integer] -> [Integer]
+sieve' _ [] = []
+sieve' limit poss =
+	let
+		h = head poss
+		t = tail poss
+		new_poss = [y | y <- t, (y `mod` h) /= 0]
+	in
+		if h > limit
+		then
+			h : new_poss
+		else
+		  h : sieve' limit new_poss
+
+prime_sieve:: Integer -> [Integer]
+prime_sieve x
+	| x < 2 = []
+	| x == 2 = [2]
+	| otherwise =
+			let
+				limit = truncate $ sqrt (fromIntegral x :: Double)
+			in
+			  sieve' limit [2..x]
+