Created
July 31, 2012 16:51
-
-
Save alts/3218394 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
($>) = flip ($) | |
-- this defintion is wrong, but is sufficient for our time range | |
leap_year y = y /= 1900 && y `mod` 4 == 0 | |
month_days m y | |
| m == 2 && leap_year y = 29 | |
| m == 2 = 28 | |
| m `elem` [4, 6, 9, 11] = 30 | |
| otherwise = 31 | |
timeline = timeline' 1 1900 | |
where timeline' 12 y = (12, y):(timeline' 1 (1+y)) | |
timeline' m y = (m, y):(timeline' (1+m) y) | |
days_since = scanl (+) 0 days | |
where days = map (uncurry month_days) (takeWhile ((2001 >) . snd) timeline) | |
first_sundays = drop 12 days_since -- we start in 1901 | |
$> filter ((6 ==) . (`mod` 7)) | |
$> length |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
($>) = flip ($) | |
fact k = foldl (*) 1 [2..k] | |
res = fact 100 | |
$> show | |
$> map (\x -> read [x]::Int) | |
$> sum |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- inefficient but sufficiently fast, 0.7s. Doing max twice the necessary amicability checks | |
($>) = flip ($) | |
divisors k = 1:(divisors' k 2 (k-1)) | |
where divisors' k t l | |
| t >= l = [] | |
| k `mod` t == 0 = t:(k `div` t):(divisors' k (t+1) (k `div` t - 1)) | |
| otherwise = divisors' k (t+1) l | |
amicability = sum . divisors | |
main = do [1..9999] | |
$> map (\v -> (v, amicability v)) | |
$> filter (uncurry (/=)) | |
$> map (\(a,b) -> (a, amicability b)) | |
$> filter (uncurry (==)) | |
$> map fst | |
$> sum | |
$> show | |
$> putStrLn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.Char | |
import qualified Data.List | |
($>) = flip ($) | |
split char "" = [] | |
split char s = chunk:(split char (if remain == "" then "" else (tail remain))) | |
where (chunk,remain) = break (== char) s | |
-- 64 = ord 'A' - 1 | |
alphavalue char = Data.Char.ord char - 64 | |
p022 vs = p022' vs 1 0 | |
where p022' [] _ acc = acc | |
p022' (x:xs) i acc = p022' xs (i+1) (acc + i*x) | |
main = do | |
raw_content <- readFile "names.txt" | |
raw_content | |
$> filter (/= '\"') | |
$> split ',' | |
$> Data.List.sort | |
$> map (sum . (map alphavalue)) | |
$> p022 | |
$> show | |
$> putStrLn |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import qualified Data.IntSet | |
($>) = flip ($) | |
divisors k = 1:(divisors' k 2 (k-1)) | |
where divisors' k t l | |
| t >= l = [] | |
| k `mod` t == 0 = let kdt = div k t in | |
if t == kdt then | |
t:(divisors' k (t+1) (kdt - 1)) | |
else | |
t:kdt:(divisors' k (t+1) (kdt - 1)) | |
| otherwise = divisors' k (t+1) l | |
sums xs = [ | |
x + y | | |
x <- xs, | |
y <- xs, | |
x + y <= 28123 | |
] | |
abundant = [12..28123] | |
$> filter (\v -> v < (sum $ divisors v)) | |
main = do | |
sums abundant | |
$> Data.IntSet.fromList | |
$> (\s -> filter ((flip Data.IntSet.notMember) s) [1..28123]) | |
$> sum | |
$> show | |
$> putStrLn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment