-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblems121-130.hs
38 lines (31 loc) · 1.5 KB
/
problems121-130.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
import Math.NumberTheory.Primes (factorise, primes)
import Data.List (sortBy, nub)
import Data.Ord (comparing)
--Could probably memoize the recursion for calculating ways but it seems
--to run fast enough as it is.
problem121 = prizeFund 15
where
prizeFund :: Integer -> Integer
prizeFund nTurns = (nOutcomes nTurns) `div` (nWins nTurns)
nWins :: Integer -> Integer
nWins nTurns = sum $ map (ways nTurns) $ [(nTurns `div` 2 + 1)..nTurns]
nOutcomes :: Integer -> Integer
nOutcomes nTurns = sum $ map (ways nTurns) $ [0..nTurns]
ways :: Integer -> Integer -> Integer
--Gives the number of ways of getting nblues
--from a game with n turns.
ways 1 0 = 1
ways 1 1 = 1
ways nTurns nBlues | nBlues > nTurns = 0
| nBlues < 0 = 0
| otherwise = nTurns*(ways (nTurns - 1) nBlues ) + --gained a red
(ways (nTurns - 1) (nBlues - 1) ) --gained a blue
problem123 = head $ dropWhile (\x -> fst x < 10^10) $
map (\x -> (sqRemainder x, fst x) ) $ zip [1..] primes
where sqRemainder (n, pn) = ((pn - 1)^n + (pn + 1)^n) `mod` pn^2
problem124 = ( sortBy (comparing snd) rads ) !! 9999
where rads = map (\x -> (x, radical x) ) [1..100000]
radical n = product $ map fst (factorise n)
problem125 = sum $ nub $ filter isPal $ concatMap sumSqNos [1..7071]
where isPal x = show x == (reverse.show) x
sumSqNos n = takeWhile (<10^8) $ tail $ scanl1 (+) $ map (^2) [n..]