-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprime_gap.hs
49 lines (38 loc) · 1.86 KB
/
prime_gap.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import Data.List
{- Deals With Input and Ouput-}
getInt :: String -> Int
getInt str = read (takeWhile (/= ' ') str)::Int
convertToHumanReadable :: [(Int, [(Int, Int)])] -> [[Char]]
convertToHumanReadable solution = map (\e -> (header (fst e))++section++(solutionData $ snd e)) solution
header :: Int -> String
header prime = "Primes found between 2 and "++(show $ prime)++": "++(show $ numPrimesTo prime)++"\n"
section :: String
section = "\nInterprime Gap Frequency\n"
solutionData :: [(Int, Int)] -> String
solutionData xs = unlines $ map (\tuple -> "\t"++(show $ fst tuple)++"\t\t"++(show $ snd tuple)) xs
{- Actual Problem Solution -}
{- Solve Prime Generation using Sieves -}
minus (x:xs) (y:ys) = case (compare x y) of
LT -> x : minus xs (y:ys)
EQ -> minus xs ys
GT -> minus (x:xs) ys
minus xs _ = xs
primesTo m = 2 : sieve [3,5..m] where
sieve [] = []
sieve (p:xs) = p : sieve (xs `minus` [p*p, p*p+2*p..m])
numPrimesTo m = length $ primesTo m
{- Solve Problem -}
interPrimeGap :: [Int] -> [Int]
interPrimeGap xs = zipWith(-)(tail xs)xs
gapAndFrequency :: [Int] -> [(Int, Int)]
gapAndFrequency xs = map (\e -> (head e, length e)) $ (group . sort) xs
{- Array of Solutions for Input Set -}
solve :: [Int] -> [(Int, [(Int, Int)])]
solve xs = map (\e -> (e, (gapAndFrequency $ interPrimeGap $ primesTo e))) xs
{- Actual Execution -}
main :: IO ()
main = do
s <- getContents
let r = map getInt (lines s)
let b = (solve r)
putStr $ unlines $ convertToHumanReadable b