Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
25
Task/Perfect-numbers/JavaScript/perfect-numbers-8.js
Normal file
25
Task/Perfect-numbers/JavaScript/perfect-numbers-8.js
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
(() => {
|
||||
const main = () =>
|
||||
enumFromTo(1, 10000).filter(perfect);
|
||||
|
||||
// perfect :: Int -> Bool
|
||||
const perfect = n => {
|
||||
const
|
||||
lows = enumFromTo(1, Math.floor(Math.sqrt(n)))
|
||||
.filter(x => (n % x) === 0);
|
||||
|
||||
return n > 1 && lows.concat(lows.map(x => n / x))
|
||||
.reduce((a, x) => (a + x), 0) / 2 === n;
|
||||
};
|
||||
|
||||
// GENERIC --------------------------------------------
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = (m, n) =>
|
||||
Array.from({
|
||||
length: n - m + 1
|
||||
}, (_, i) => i + m)
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue