Skip to content

Instantly share code, notes, and snippets.

View fbiville's full-sized avatar

Florent Biville fbiville

View GitHub Profile
take 10 $ show $ sum [37107287533902102798797998220837590246510135740250,46376937677490009712648124896970078050417018260538,74324986199524741059474233309513058123726617309629,91942213363574161572522430563301811072406154908250,23067588207539346171171980310421047513778063246676,89261670696623633820136378418383684178734361726757,28112879812849979408065481931592621691275889832738,44274228917432520321923589422876796487670272189318,47451445736001306439091167216856844588711603153276,70386486105843025439939619828917593665686757934951,62176457141856560629502157223196586755079324193331,64906352462741904929101432445813822663347944758178,92575867718337217661963751590579239728245598838407,58203565325359399008402633568948830189458628227828,80181199384826282014278194139940567587151170094390,35398664372827112653829987240784473053190104293586,86515506006295864861532075273371959191420517255829,71693888707715466499115593487603532921714970056938,54370070576826684624621495650076471787294438377604,532826541087568284431911906346940
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
@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))
@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)
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 ]
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
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))
let range = [1..100] in (foldl (+) 0 range)^2 - (foldl (+) 0 (map (^2) range))
20 * (1+length (takeWhile (/= True) (map (foldl (&&) True) (map (\n -> (map (\m -> n `mod` m == 0) [1..20])) [20,40..]))))
import Data.Maybe
import Data.List
primeFactors :: Int -> [Int]
primeFactors = (filter prime) . divisors
prime :: Int -> Bool
prime a =
let upper = floor (sqrt (fromIntegral a)) + 1 in
(a `mod` 2 /= 0) && isNothing (find (isMultiple a) [3,5..upper])