Skip to content

Instantly share code, notes, and snippets.

View fbiville's full-sized avatar

Florent Biville fbiville

View GitHub Profile
primes :: Int -> Int
primes index = last (_primes 0 (index-1) [2] [3,5..])
_primes :: Int -> Int -> [Int] -> [Int] -> [Int]
_primes cur max p x
| cur >= max = p
| otherwise =
let x0 = head x in
_primes (cur+1) max (p++[x0]) (filter (\n -> n `mod` x0 /= 0) (tail x))
import Data.Char
largestProduct :: Int -> String -> (Int, [Int])
largestProduct size n =
let digits = map digitToInt n in
findSubsequence size (0, [0]) digits
findSubsequence :: Int -> (Int,[Int]) -> [Int] -> (Int, [Int])
findSubsequence size prev seq
| length seq < size = prev
head [ (a*b*c,a,b,c) | c <- [3..1000], b <- [2..(c-1)], a <- [1..(b-1)], a^2+b^2 == c^2, a+b+c == 1000 ]
@fbiville
fbiville / Euler_10.hs
Created January 4, 2015 22:15
Warning: super slow (when >= 10^6)
sumPrimes :: Int -> Int
sumPrimes limit = _primes limit 2 [3,5..limit]
_primes :: Int -> Int -> [Int] -> Int
_primes limit sum [] = sum
_primes limit sum x = let x0=(head x) in _primes limit (sum+x0) (filter (\n -> n `mod` x0 /= 0) x)
@fbiville
fbiville / Euler_11.hs
Created January 5, 2015 10:32
Warning: dumb (& dumber)
greatestProduct :: [[Int]] -> Int
greatestProduct mat = _greatestProduct (0,0) mat 0
_greatestProduct :: (Int,Int) -> [[Int]] -> Int -> Int
_greatestProduct (20,y) mat prev = prev
_greatestProduct coord mat prev
| current > prev = _greatestProduct nextC mat current
| otherwise = _greatestProduct nextC mat prev
where nextC = (nextCoord coord)
maxVert = (max (up coord mat) (down coord mat))
module Euler12 (triangle,multipleCount) where
import Data.List
triangle :: Int -> Int
triangle n = head . dropWhile (\m -> multipleCount m < n) $ [ i*(i+1) `div` 2 | i <- [1..] ]
multipleCount :: Int -> Int
multipleCount number =
product $ map (+1) $ map length (group pFactors)
where pFactors = primeFactors [] number 2
take 10 $ show $ sum [37107287533902102798797998220837590246510135740250,46376937677490009712648124896970078050417018260538,74324986199524741059474233309513058123726617309629,91942213363574161572522430563301811072406154908250,23067588207539346171171980310421047513778063246676,89261670696623633820136378418383684178734361726757,28112879812849979408065481931592621691275889832738,44274228917432520321923589422876796487670272189318,47451445736001306439091167216856844588711603153276,70386486105843025439939619828917593665686757934951,62176457141856560629502157223196586755079324193331,64906352462741904929101432445813822663347944758178,92575867718337217661963751590579239728245598838407,58203565325359399008402633568948830189458628227828,80181199384826282014278194139940567587151170094390,35398664372827112653829987240784473053190104293586,86515506006295864861532075273371959191420517255829,71693888707715466499115593487603532921714970056938,54370070576826684624621495650076471787294438377604,532826541087568284431911906346940
import Data.Char
sum $ map digitToInt $ show (2^1000)
main :: IO ()
main = print $ countLetters [1..1000]
countLetters :: [Int] -> Int
countLetters = sum . (map (length . translate))
translate :: Int -> String
translate 0 = ""
translate 1 = "one"
translate 2 = "two"
main :: IO ()
main = print $ maximum_path_sum [[75],[95,64],[17,47,82],[18,35,87,10],[20,4,82,47,65],[19,1,23,75,3,34],[88,2,77,73,7,63,67],[99,65,4,28,6,16,70,92],[41,41,26,56,83,40,80,70,33],[41,48,72,33,47,32,37,16,94,29],[53,71,44,65,25,43,91,52,97,51,14],[70,11,33,28,77,73,17,78,39,68,17,57],[91,71,52,38,17,14,91,43,58,50,27,29,48],[63,66,4,68,89,53,67,30,73,16,69,87,40,31],[4,62,98,27,23,9,70,98,73,93,38,53,60,4,23]]
maximum_path_sum :: [[Int]] -> Int
maximum_path_sum = sum . (maximum_path [] (0,0))
maximum_path :: [Int] -> (Int,Int) -> [[Int]] -> [Int]
maximum_path acc i@(x,y) paths
| x == (length paths)-1 = acc'
| otherwise = maximum_path acc' i' paths