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-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))) | |
) |
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 (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'] |
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 (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 |
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 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 |
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 (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) |
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 (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 |
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 (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から始めると分かっているのでそれ以前の値は考慮しない |
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 (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] |
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-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))) | |
) |
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))) | |
) |