Skip to content

Instantly share code, notes, and snippets.

@yen3
Last active December 25, 2015 14:19
Show Gist options
  • Save yen3/6990102 to your computer and use it in GitHub Desktop.
Save yen3/6990102 to your computer and use it in GitHub Desktop.
Project Euler
sol1 = takeWhile (<1000) $ filter (\x -> x `mod` 3 == 0 || x `mod` 5 == 0) [1..]
sol2 = takeWhile (<1000) [x| x<-[1..], x `mod` 3 ==0 || x `mod` 5 ==0]
print reduce(lambda y, x: y + [x] if x%3 == 0 or x% 5 == 0 else y, xrange(3, 1000), [])
def m35():
n = [3, 5]
while (len(n) <= 100):
n.append(min(filter(lambda x: x>n[-1], reduce(lambda y, x: y + [x*3, x*5], n, []))))
return n
n = m35()
print n
import qualified Data.Map as Map
-- The answer for Problem 5
smallestMultiple :: Integer -> Integer
smallestMultiple n = Map.foldWithKey (\k v sm-> sm * (power k v)) 1 (numFactorMapList n)
numFactorMapList :: Integer -> Map.Map Integer Integer
numFactorMapList n = foldr mergeMap (Map.fromList []) (map numFactorMap [2..n])
mergeMap :: Map.Map Integer Integer -> Map.Map Integer Integer -> Map.Map Integer Integer
mergeMap = Map.unionWithKey (\k v1 v2-> if v1 > v2 then v1 else v2)
numFactorMap :: Integer -> Map.Map Integer Integer
numFactorMap = flip get_num_factor_map $ (Map.fromList [])
get_num_factor_map :: Integer -> Map.Map Integer Integer -> Map.Map Integer Integer
get_num_factor_map 0 m = m
get_num_factor_map 1 m = m
get_num_factor_map n m =
if Map.member factor m then
get_num_factor_map next_n (updateValue m)
else
get_num_factor_map next_n (insertValue m)
where next_n = quot n factor
factor = getFirstFactor n
insertValue m = Map.insert factor 1 m
updateValue m = snd $ Map.updateLookupWithKey (\k v-> if k == factor then Just (v+1) else Nothing) factor m
getFirstFactor :: Integer -> Integer
getFirstFactor = flip get_first_factor $ 2
get_first_factor :: Integer -> Integer -> Integer
get_first_factor 0 _ = 0
get_first_factor 1 _ = 1
get_first_factor 2 _ = 2
get_first_factor n p = if n `mod` p == 0 then p else get_first_factor n (p+1)
power _ 0 = 1
power 0 _ = 0
power x n = let sqr k = k * k
half_n = n `div` 2
sqrHalfPower = sqr ( power x half_n )
in if even n then sqrHalfPower else x * sqrHalfPower
import Data.Char
sumNumStrList :: String -> String
sumNumStrList = numListToString . adjustDec . addNumList . getNumList
getNumList:: String -> [[Int]]
getNumList s = map getNumAsList $ lines s
where getNumAsList = reverse . map (\x -> Data.Char.ord x - 48)
addNumList :: [[Int]] -> [Int]
addNumList = foldr1 (zipWith (+))
adjustDec :: [Int] -> [Int]
adjustDec = (flip adjustDec') 0
adjustDec' :: [Int] -> Int -> [Int]
adjustDec' [] carry_in =
if carry_in > 10 then (carry_in `mod` 10) : (adjustDec' [] (carry_in `quot` 10))
else carry_in : []
adjustDec' (x:xs) carry_in = ((x + carry_in) `mod` 10): (adjustDec' xs ((x + carry_in) `quot` 10))
numListToString :: [Int] -> String
numListToString nl = reverse $ map (\x -> Data.Char.chr (x+48)) nl
main :: IO()
main = do
contents <- readFile "pe13.input"
let result = sumNumStrList contents
putStrLn result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment