Last active
August 29, 2015 14:05
-
-
Save zaneli/d03bbc0393bca90a3f32 to your computer and use it in GitHub Desktop.
「HaskellでProject Euler(Problem 46~48)」ブログ用
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から始めると分かっているのでそれ以前の値は考慮しない | |
isComposite n = n /= (head $ primesLimitNum n) | |
isGoldbachConjecture :: Integral a => a -> Bool | |
isGoldbachConjecture n = isJust $ find (\m -> n == m) [f x y | x <- primesLimitNum (n-2), y <- [halfSqrt n, (halfSqrt n)-1 .. 1]] | |
where | |
f n m = truncate $ (fromIntegral n) + 2 * (fromIntegral m) ** 2 | |
halfSqrt n = truncate $ sqrt $ fromIntegral $ n`div`2 |
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 Data.Maybe (fromJust) | |
import Zaneli.Euler (primeFactors) | |
main = print $ head $ fromJust $ distinctPrimesFactors 4 | |
distinctPrimesFactors :: Integral a => Int -> Maybe [a] | |
distinctPrimesFactors n = find (all (\m -> (length $ primeFactors m) >= n)) $ consecutives n | |
consecutives :: (Enum a, Num a) => Int -> [[a]] | |
consecutives n = unfoldr (\ns -> Just (take n ns, tail ns)) [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
import Zaneli.Euler (listToNum, numToList) | |
main = print $ sumOfSelfPowers [1..1000] | |
limit :: Int | |
limit = 10 | |
sumOfSelfPowers :: [Int] -> Int | |
sumOfSelfPowers ns = foldl (\acc n -> drop' $ acc + selfPower n) 0 ns | |
selfPower :: Int -> Int | |
selfPower n = foldl (\acc n -> drop' $ acc * n) 1 $ replicate n n | |
drop' :: Int -> Int | |
drop' n | (length n') <= limit = n | |
| otherwise = listToNum $ drop (length n' - limit) n' | |
where n' = numToList 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 $ drop' $ sum $ map selfPower [1..1000] | |
selfPower :: Int -> Int | |
selfPower n = iterate (drop' . (* n)) 1 !! n | |
drop' :: Integral a => a -> a | |
drop' n = n `mod` 10 ^ limit | |
where limit = 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment