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
euler1 = sumMultiples 1000 [3, 5] | |
sumMultiples :: Integral a => a -> [a] -> a | |
sumMultiples _ [] = 0 | |
sumMultiples n muls = foldl1 (+) [x | x <- [1..(n-1)], any (\y -> x `mod` y == 0) muls] |
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 Data.List | |
euler4 = maxPalindrome 100 999 | |
maxPalindrome :: (Enum a, Num a, Ord a, Show a) => a -> a -> a | |
maxPalindrome n m = | |
let list = sortBy (\x y -> compare y x) [x * y | x <- [n..m], y <- [n..m]] in | |
head $ filter isPalindrome list | |
where | |
isPalindrome x = let s = show x in s == reverse s |
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
main = print $ prime 10001 | |
prime :: Integral a => Int -> a | |
prime 1 = 2 | |
prime n = head $ prime' 3 [] | |
where | |
prime' m list | length list >= (n-1) = list | |
| isPrime list = prime' (m + 2) (m:list) | |
| otherwise = prime' (m + 2) list | |
where isPrime = all (\x -> m `mod` x /= 0) . dropWhile (\x -> x ^ 2 > m) |
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
print "問題1.\n"; | |
my @array = ('Alice', 'Bob'); | |
unshift @array, "Amon2"; | |
push @array, "Catalyst"; |
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
main = print $ sum $ primes 2000000 | |
primes :: Integral a => a -> [a] | |
primes n | |
| n < 2 = [] | |
| otherwise = primes' 3 [2] [] | |
where | |
primes' m list list' | |
| m > n = list | |
| isPrime list' = primes' (m + 2) (m:list) (list' ++ [m]) |
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
main = print $ sumHead 10 | |
sumHead :: (Read a, Num a) => Int -> a | |
sumHead n = read $ take n $ show $ sum nums | |
nums = [ | |
37107287533902102798797998220837590246510135740250, | |
46376937677490009712648124896970078050417018260538, | |
74324986199524741059474233309513058123726617309629, | |
91942213363574161572522430563301811072406154908250, |
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 Data.Char | |
main = print $ digitSum 1000 | |
digitSum :: Integral a => a -> Int | |
digitSum n = sum $ map digitToInt $ show $ 2 ^ n |
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
main = print $ length $ filter (== Sun) firstOfTheMonthWeek | |
firstOfTheMonthWeek :: [Week] | |
firstOfTheMonthWeek = firstOfTheMonthWeek' 1900 1 [Mon] | |
where | |
firstOfTheMonthWeek' 2000 12 ws = ws | |
firstOfTheMonthWeek' y m ws@(w:_) = firstOfTheMonthWeek' y' m' $ ws' (week w $ dayNums y m) | |
where | |
(y', m') = nextMonth y m | |
ws' w | y == 1900 = [w] -- 1901年からの日曜日の個数を求めたいため、1900年の場合は次の月の曜日だけを再帰の引数に使用する |
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 Data.Char (ord) | |
import Data.List (sort) | |
import Text.Regex (matchRegexAll, mkRegex) | |
main = do names <- readFile "names.txt" | |
print $ scoreSum $ sort $ listNames names | |
-- ダブルクオートで囲まれたカンマ区切りの文字列をリストにする | |
listNames :: String -> [String] | |
listNames names = matchNames [] names |
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 Data.Maybe (catMaybes) | |
data MyType = I Int | F Float | C Char | |
deriving (Show) | |
-- 再帰を使う | |
filterC :: [MyType] -> [MyType] | |
filterC [] = [] | |
filterC (x@(C _):xs) = x:filterC xs | |
filterC (_:xs) = filterC xs |