Skip to content

Instantly share code, notes, and snippets.

View zaneli's full-sized avatar
🤔
...?

Shunsuke Otani zaneli

🤔
...?
View GitHub Profile
@zaneli
zaneli / euler1.hs
Last active December 20, 2015 12:29
「HaskellでProject Euler(Problem 1~3)」ブログ用
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]
@zaneli
zaneli / euler4.hs
Last active December 20, 2015 15:39
「HaskellでProject Euler(Problem 4~6)」ブログ用
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
@zaneli
zaneli / euler7-1.hs
Last active December 20, 2015 22:19
「HaskellでProject Euler(Problem 7~9)」ブログ用
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)
@zaneli
zaneli / array.pl
Last active December 21, 2015 16:09
Perl 入学式 #3 用
#!/usr/bin/env perl
use strict;
use warnings;
print "問題1.\n";
my @array = ('Alice', 'Bob');
unshift @array, "Amon2";
push @array, "Catalyst";
@zaneli
zaneli / euler10.hs
Last active December 24, 2015 05:28
「HaskellでProject Euler(Problem 10~12)」ブログ用
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])
@zaneli
zaneli / euler13.hs
Last active December 25, 2015 21:39
「HaskellでProject Euler(Problem 13~15)」ブログ用
main = print $ sumHead 10
sumHead :: (Read a, Num a) => Int -> a
sumHead n = read $ take n $ show $ sum nums
nums = [
37107287533902102798797998220837590246510135740250,
46376937677490009712648124896970078050417018260538,
74324986199524741059474233309513058123726617309629,
91942213363574161572522430563301811072406154908250,
@zaneli
zaneli / euler16.hs
Last active December 26, 2015 23:39
「HaskellでProject Euler(Problem 16~18)」ブログ用
import Data.Char
main = print $ digitSum 1000
digitSum :: Integral a => a -> Int
digitSum n = sum $ map digitToInt $ show $ 2 ^ n
@zaneli
zaneli / euler19.hs
Last active December 30, 2015 00:39
「HaskellでProject Euler(Problem 19~21)」ブログ用
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年の場合は次の月の曜日だけを再帰の引数に使用する
@zaneli
zaneli / euler22-1.hs
Last active January 2, 2016 14:19
「HaskellでProject Euler(Problem 22~24)」ブログ用
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
@zaneli
zaneli / MyType.hs
Last active January 3, 2016 22:59
すごいHaskellたのしく学ぼう輪読会用
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