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 $ sum [x | x <- [1..999], mod x 3 == 0 || mod x 5 == 0] |
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
| fibs = 1 : 2 : zipWith (+) fibs (tail fibs) | |
| main = print $ sum [x| x <- takeWhile (< 4000000) fibs, even x] |
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
| primeFactors n = primeFactors' n 2 | |
| where | |
| primeFactors' n f | |
| | f ^ 2 > n = [n] | |
| | n `mod` f == 0 = f : primeFactors' (n `div` f) f | |
| | otherwise = primeFactors' n (f + 1) | |
| main = print $ maximum $ primeFactors 600851475143 |
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
| euler4(Answer) :- | |
| findall(X, palindromes(X), Palindromes), | |
| max_list(Palindromes, Answer). | |
| palindromes(Z) :- | |
| between(100, 999, X), | |
| between(X, 999, Y), | |
| Z is X * Y, | |
| is_palindrome(Z). |
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 $ foldl lcm 1 [1..20] |
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 $ (sum [1..100] ^ 2) - (sum [x ^ 2| x <- [1..100]]) |
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
| euler7 = primes !! 10000 | |
| primes = 2 : 3 : [x| x <- [5, 7..], is_prime x] | |
| is_prime n = is_prime' [3, 5..] | |
| where | |
| is_prime' (x : xs) | |
| | n `mod` x == 0 = False | |
| | x ^ 2 > n = True | |
| | otherwise = is_prime' 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
| import Data.Char (digitToInt) | |
| euler8 = maximum $ accumulate $ map digitToInt given | |
| accumulate numbers | |
| | length numbers < 5 = [] | |
| | otherwise = product (take 5 numbers) : accumulate (drop 1 numbers) | |
| given = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850 |
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
| euler9(Answer) :- | |
| between(1, 999, A), | |
| between(A, 999, B), | |
| C is 1000 - A - B, | |
| is_pythagorean_triplet(A, B, C), | |
| Answer is A * B * C. | |
| is_pythagorean_triplet(A, B, C) :- | |
| A < B, B < C, | |
| A ^ 2 + B ^ 2 =:= C ^ 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
| euler10 = sum $ takeWhile (< 2000000) (2 : 3 : findPrimes [3] 5) | |
| findPrimes primes number | |
| | isNotPrime = findPrimes primes (number + 2) | |
| | otherwise = number : findPrimes (number : primes) (number + 2) | |
| where | |
| isNotPrime = any isModulable [p| p <- primes, p ^ 2 <= number] | |
| isModulable p = number `mod` p == 0 |
OlderNewer