Last active
July 28, 2017 06:09
-
-
Save marcmartino/743bfb89ef2aa928542bcca614277d04 to your computer and use it in GitHub Desktop.
Projet Euler 1
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
//jshint esnext:true | |
/* | |
Multiples of 3 and 5 - Problem 1 | |
If we list all the natural numbers below 10 that are multiples of 3 or 5, | |
we get 3, 5, 6 and 9. The sum of these multiples is 23. | |
Find the sum of all the multiples of 3 or 5 below 1000. | |
*/ | |
// getMultiples :: Integer -> Integer -> [Integer] | |
const getMultiples = R.curry((max, mult) => R.compose(R.map(R.multiply(mult)), | |
R.range(1))(parseInt(max / mult) + 1)); | |
// sumOfMultiples :: Integer -> [Integer] -> Integer | |
const sumOfMultiples = max => R.compose( | |
R.sum, | |
R.uniq, | |
R.apply(R.concat), | |
R.map(getMultiples(max))); | |
console.log(sumOfMultiples(1000)([5,3])); |
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
//jshint esnext:true | |
/* | |
Multiples of 3 and 5 - Problem 1 | |
If we list all the natural numbers below 10 that are multiples of 3 or 5, | |
we get 3, 5, 6 and 9. The sum of these multiples is 23. | |
Find the sum of all the multiples of 3 or 5 below 1000. | |
*/ | |
// isMultiple :: Integer -> (Integer -> Bool) | |
const isMultiple = mult => R.compose(R.equals(0), (R.modulo(R.__, mult))); | |
// multiplesCheck :: [Integer] -> (Integer -> Bool) | |
const multiplesCheck = R.compose(R.anyPass, R.map(isMultiple)); | |
// sumOfMultiplesOr :: Integer -> [Integer] -> Integer | |
const sumOfMultiplesOr = max => multiples => R.compose( | |
R.sum, | |
R.filter(multiplesCheck(multiples)), | |
R.range(1))(max); | |
console.log(sumOfMultiplesOr(1000)([3, 5])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment