Skip to content

Instantly share code, notes, and snippets.

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

Shunsuke Otani zaneli

🤔
...?
View GitHub Profile
@zaneli
zaneli / p26.clj
Last active August 29, 2015 14:15
「ClojureでNinety-Nine Lisp Problems(P26~28)」ブログ用
(defn my-combination
"Generate the combinations of K distinct objects chosen from the N elements of a list."
([n xs] (my-combination n xs nil))
([n xs ys]
(if (== n 0)
(list (reverse ys))
(mapcat
#(let [[z & zs] (drop % xs)] (my-combination (dec n) zs (cons z ys)))
(range 0 (- (count xs) (dec n)))
)
@zaneli
zaneli / euler61-1.hs
Last active August 29, 2015 14:15
「HaskellでProject Euler(Problem 61~63)」ブログ用
import Data.List (find)
import Data.Maybe (maybeToList)
main = print $ sum search
search :: [Integer]
search = fst $ head $ appendCyclics $ map (\o -> ([o], candidates)) octagonals
appendCyclics :: (Show a, Eq b) => [([a], [(a, b)])] -> [([a], [(a, b)])]
appendCyclics nss | Just r' <- r = [r']
@zaneli
zaneli / euler58.hs
Last active August 29, 2015 14:14
「HaskellでProject Euler(Problem 58~60)」ブログ用
import Data.List (find, unfoldr)
import Zaneli.Euler (isPrime)
main = let Just (_, l) = search spiralDiagonals in print l
spiralDiagonals :: [(Integer, [Integer])]
spiralDiagonals = zip [3,5..] $ unfoldr spiralDiagonals' (1, 0)
where
spiralDiagonals' (n, step) = Just (ns, (m, step'))
where
@zaneli
zaneli / euler55.hs
Last active August 29, 2015 14:13
「HaskellでProject Euler(Problem 55~57)」ブログ用
main = print $ length $ filter isLychrel [1..9999]
isLychrel :: Integer -> Bool
isLychrel = not . any isPalindrome . take 49 . tail . iterate addReverseNum
addReverseNum :: (Show a, Read a, Num a) => a -> a
addReverseNum n = n + reverseNum n
where reverseNum = read . reverse . show
isPalindrome :: Show a => a -> Bool
@zaneli
zaneli / euler52-1.hs
Last active August 29, 2015 14:13
「HaskellでProject Euler(Problem 52~54)」ブログ用
import Data.List (find, sort)
main = let Just result = permuted [2..6] in print result
permuted :: (Enum a, Num a, Show a) => [a] -> Maybe a
permuted ns = find (\n -> all (sameSign n) $ map (* n) ns) [1..]
sameSign :: (Show a, Show b) => a -> b -> Bool
sameSign x y = sort (show x) == sort (show y)
@zaneli
zaneli / euler49-1.hs
Last active August 29, 2015 14:12
「HaskellでProject Euler(Problem 49~51)」ブログ用
import Data.Maybe (fromJust)
import Data.List (find, sort)
import Zaneli.Euler (primesLimitNum)
main = print $ show' $ head $ fromJust primePermutations
where show' (n1, n2, n3) = (show n1) ++ (show n2) ++ (show n3)
primes :: [Integer]
primes = reverse $ takeWhile (>= 1000) $ primesLimitNum 9999
@zaneli
zaneli / euler46.hs
Last active August 29, 2015 14:05
「HaskellでProject Euler(Problem 46~48)」ブログ用
import Data.List (find)
import Data.Maybe (fromJust, isJust)
import Zaneli.Euler (primesLimitNum)
main = print $ fromJust $ find (not . isGoldbachConjecture) oddComposites
oddComposites :: [Integer]
oddComposites = filter isComposite [3,5..]
where
-- これだけでは 0 や 1 も True と判定されるが、今回3から始めると分かっているのでそれ以前の値は考慮しない
@zaneli
zaneli / euler43-1.hs
Last active August 29, 2015 14:05
「HaskellでProject Euler(Problem 43~45)」ブログ用
import Data.List (permutations)
import Zaneli.Euler (primesLimitIndex)
main = print $ sum [read pand | pand <- pands, isSubStrDivisibility pand]
where pands = permutations ['0'..'9']
isSubStrDivisibility :: String -> Bool
isSubStrDivisibility pand = all (\(n, m) -> n `mod` m == 0) $ zip subStrs primes
where
subStrs = map (\n -> read $ take 3 $ drop n pand) [1..7]
@zaneli
zaneli / p21-1.clj
Last active August 29, 2015 14:00
「ClojureでNinety-Nine Lisp Problems(P21~25)」ブログ用
(defn my-insert-at
"Insert an element at a given position into a list."
[x xs n]
(reverse (first (reduce (fn [[elems cnt] elem]
[(if (not= cnt n) (cons elem elems) (cons elem (cons x elems))), (inc cnt)]
) [nil, 1] xs)))
)
@zaneli
zaneli / p16-1.clj
Last active August 29, 2015 13:59
「ClojureでNinety-Nine Lisp Problems(P16~20)」ブログ用
(defn my-drop
"Drop every N'th element from a list."
[xs n]
(map #(second %) (filter #(not= (mod (first %) n) (dec n)) (map-indexed vector xs)))
)