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 Zaneli.Euler (fib) | |
main = print $ fst $ head $ dropWhile (\(_, n) -> n < 10^(1000-1)) $ zip [1..] $ map fib [1..] |
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 $ 1 + addDiags [2..1001*1001] 0 1 0 | |
addDiags :: (Num a, Num b, Eq b) => [a] -> a -> Int -> b -> a | |
addDiags [] sum _ _ = sum | |
addDiags list sum step count = let (n:ns) = drop step' list in | |
addDiags ns (sum + n) step' count' | |
where | |
(step', count') | count == 4 = (step + 2, 1) | |
| otherwise = (step, count + 1) |
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 $ coinSums 200 | |
coinSums :: Integer -> [(Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)] | |
coinSums n = [ (po1, po2, po5, po10, po20, po50, pe1, pe2) | | |
po1 <- [0,1..n], | |
po2 <- [0,2..n-po1], | |
po5 <- [0,5..n-(po1+po2)], | |
po10 <- [0,10..n-(po1+po2+po5)], | |
po20 <- [0,20..n-(po1+po2+po5+po10)], | |
po50 <- [0,50..n-(po1+po2+po5+po10+po20)], |
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.Array (listArray, (!)) | |
import Data.List (find) | |
import Zaneli.Euler (numToList) | |
main = print $ sum digitFactorials | |
digitFactorials :: [Int] | |
digitFactorials = [n | n <- [3..limit], let ns = map (facts !) $ numToList n, (sum ns) == n] | |
where | |
facts = let (minIdx, maxIdx) = (0, 9) in listArray (minIdx, maxIdx) $ map fact [minIdx..maxIdx] -- 一桁の値の階乗を予め作っておく |
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 Zaneli.Euler (numToList, listToNum) | |
main = print $ sum $ trunPrimes 11 | |
trunPrimes :: Int -> [Int] | |
trunPrimes limit = trunPrimes' 3 [2] [] | |
where | |
trunPrimes' n primes result | |
| length result >= limit = result | |
| isPrime n = trunPrimes' (n + 2) (primes ++ [n]) result' |
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 (digitToInt) | |
main = print $ product $ pickUpChampernowne $ map (10^) [0..6] | |
pickUpChampernowne :: [Int] -> [Int] | |
pickUpChampernowne targets = pickUp 1 targets 0 [] | |
where | |
pickUp _ [] _ nums = nums | |
pickUp n targets@(x:xs) count nums | |
| inBetween = pickUp (n+1) xs count' (targetNum:nums) |
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
(defn my-last | |
"Find the last box of a list." | |
[xs] | |
(if (>= 1 (count xs)) | |
xs | |
(my-last (rest xs)) | |
) | |
) |
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
(defn my-palindrome? | |
"Find out whether a list is a palindrome." | |
[word] | |
(let [middle (/ (count word) 2), word1 (take middle word), word2 (reverse (drop middle word))] | |
(every? #(= (first %) (second %)) (map list word1 word2)) | |
) | |
) |
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
(defn my-encode | |
"Run-length encoding of a list." | |
[xs] | |
(map #(list (count %) (first %)) (partition-by identity xs)) | |
) | |
(defn my-encode-modified | |
"Modified run-length encoding." | |
[xs] | |
(map #(let [[cnt elem] %] (if (== cnt 1) elem (list cnt elem))) (my-encode xs)) |
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
(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))) | |
) |