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.Array | |
| verticals = iterate (\(x, y) -> (x, y + 1)) | |
| horizontals = iterate (\(x, y) -> (x + 1, y)) | |
| rdiagonals = iterate (\(x, y) -> (x + 1, y + 1)) | |
| ldiagonals = iterate (\(x, y) -> (x - 1, y + 1)) | |
| products grid = [ | |
| product values | | |
| base <- bases, |
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 | |
| main = print $ head $ filter ((> 500) . countDivisors) triangleNumbers | |
| triangleNumbers = scanl1 (+) [1..] | |
| countDivisors n = product $ map ((+1) . length) (group (primeFactors n)) | |
| primeFactors n = primeFactors' n 2 | |
| where |
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 . take 10 . show . sum $ given | |
| given = [ | |
| 37107287533902102798797998220837590246510135740250, | |
| 46376937677490009712648124896970078050417018260538, | |
| 74324986199524741059474233309513058123726617309629, | |
| 91942213363574161572522430563301811072406154908250, | |
| 23067588207539346171171980310421047513778063246676, | |
| 89261670696623633820136378418383684178734361726757, | |
| 28112879812849979408065481931592621691275889832738, |
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.Array | |
| import Data.List | |
| import Data.Ord (comparing) | |
| main = print $ maximumBy (comparing snd) $ assocs $ collatz 1000000 | |
| collatz size = memo | |
| where | |
| memo = listArray (1, size) $ 1 : [1 + count n | n <- [2..size]] | |
| count 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
| (define (equal? xs ys) | |
| (cond ((and (null? xs) (null? ys)) #t) | |
| (else (and (not (null? xs)) (not (null? ys)) | |
| (eq? (car xs) (car ys)) | |
| (equal? (cdr xs) (cdr ys)))))) |
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
| (/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 3))))) | |
| (* 3 (- 6 2) (- 2 7))) |
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
| (define | |
| (sum-square x y z) | |
| (cond ((and (< z x) (< z y)) (+ (* x x) (* y y))) | |
| ((and (< y x) (< y z)) (+ (* x x) (* z z))) | |
| (else (+ (* y y) (* z 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
| (define (union-set set1 set2) | |
| (cond ((null? set1) set2) | |
| ((element-of-set? (car set1) set2) (union-set (cdr set1) set2)) | |
| (else (cons (car set1) | |
| (union-set (cdr set1) set2))))) |
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
| (define (union-set set1 set2) | |
| (cond ((null? set1) set2) | |
| ((null? set2) set1) | |
| (else (let ((x1 (car set1)) (x2 (car set2))) | |
| (cond ((< x1 x2) (cons x1 (union-set (cdr set1) set2))) | |
| ((> x1 x2) (cons x2 (union-set set1 (cdr set2)))) | |
| (else (cons x1 (union-set (cdr set1) (cdr set2))))))))) |
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
| combination n r = factorial n `div` (factorial (n - r) * factorial r) | |
| where factorial 1 = 1 | |
| factorial n = n * factorial (n - 1) | |
| main = print $ combination 40 20 |