Skip to content

Instantly share code, notes, and snippets.

@2GMon
2GMon / dailycoding18_.hs
Created August 9, 2012 05:06
Project Euler : Problem 18
-- Project Euler : Problem 18
--
-- 総当たり回避
-- 三角形をひっくり返して,1つ下のリストに上のリストを足していく
triangle :: [[Int]]
triangle = [
[75],
[95, 64],
[17, 47, 82],
@2GMon
2GMon / factorize.hs
Created August 10, 2012 02:48
素因数分解とか約数とか
import Data.List
-- [a]と[b]の全ての組み合わせをfで計算するzipWithみたいなもの
-- 例: zipWith' (*) [1,2,4,8] [1,3]
-- >> [1,3,2,6,4,12,8,24]
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ _ [] = []
zipWith' _ [] _ = []
zipWith' f (a:as) b = (map (f a) b) ++ (zipWith' f as b)
@2GMon
2GMon / dailycoding19.hs
Created August 10, 2012 04:15
Project Euler : Problem 19
-- Project Euler : Problem 19
isBissextile :: Int -> Bool
isBissextile y
| (y `mod` 400 /= 0) && (y `mod` 100 == 0) = False
| y `mod` 4 == 0 = True
| otherwise = False
-- y年m月の日数
daysOfMonth :: Int -> Int -> Int
@2GMon
2GMon / dailycoding21.hs
Created August 12, 2012 07:40
Project Euler : Problem 21
-- Project Euler : Problem 21
import Data.List
-- [a]と[b]の全ての組み合わせをfで計算するzipWithみたいなもの
-- 例: zipWith' (*) [1,2,4,8] [1,3]
-- >> [1,3,2,6,4,12,8,24]
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ _ [] = []
zipWith' _ [] _ = []
@2GMon
2GMon / dailycoding22.hs
Created August 13, 2012 06:40
Project Euler : Problem 22
-- Project Euler : Problem 22
import Data.List
import Data.Char
import System.IO
alphabetScore :: Char -> Int
alphabetScore c = ord c - ord 'A' + 1
nameScore :: String -> Int
@2GMon
2GMon / dailycoding23.hs
Created August 14, 2012 01:58
Project Euler : Problem 23
-- Project Euler : Problem 23
import Data.List
-- [a]と[b]の全ての組み合わせをfで計算するzipWithみたいなもの
-- 例: zipWith' (*) [1,2,4,8] [1,3]
-- >> [1,3,2,6,4,12,8,24]
zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
zipWith' _ _ [] = []
zipWith' _ [] _ = []
@2GMon
2GMon / dailycoding24.hs
Created August 15, 2012 02:23
Project Euler : Problem 24
-- Project Euler : Problem 24
-- 10の倍数
multipleOf10 :: Int -> [Int]
multipleOf10 n = zipWith (^) (repeat 10) [0..n]
-- [a]の順列のInt番目
permutaion :: (Eq a) => [a] -> Int -> [a]
permutaion [] _ = []
permutaion [x] _ = [x]
@2GMon
2GMon / dailycoding25.hs
Created August 16, 2012 05:37
Project Euler : Problem 25
-- Project Euler : Problem 25
fibonacci :: [Integer]
fibonacci = 1:fib 1 2
where fib a b = a:fib b (a + b)
main = print $ fst $ head $ dropWhile (\(x, y) -> y < 1000) $
map (\(x, y) -> (x, length $ show $ y)) $ zip [1..] fibonacci
-- 4782
@2GMon
2GMon / dailycoding29.hs
Created August 20, 2012 05:43
Project Euler : Problem 29
-- Project Euler : Problem 29
import Data.List
main = print $ length $ map head $ group $ sort [a^b | a <- [2..100], b <- [2..100]]
-- nubよりもmap head $ group の方が速かった
-- 9183
-- ./dailycoding29 0.02s user 0.01s system 92% cpu 0.029 total
@2GMon
2GMon / dailycoding30.hs
Created August 21, 2012 03:24
Project Euler : Problem 30
-- Project Euler : Problem 30
import Data.Char
sumFactorialOf5 :: Int -> Int
sumFactorialOf5 = sum . map (^5) . map digitToInt . show
main = print $ sum [a | a <- [2..9^5*6], a == sumFactorialOf5 a]
-- 443839