Proper divisors: Difference between revisions
→{{header|JavaScript}}: ES6
m (→version 3: indented a comment to align with the others.) |
(→{{header|JavaScript}}: ES6) |
||
Line 1,115:
=={{header|JavaScript}}==
===ES5===
<lang JavaScript>(function () {
Line 1,216 ⟶ 1,218:
{"n":15120,"divisorCount":79}
===ES6===
<lang JavaScript>(() => {
// properDivisors :: Int -> [Int]
let properDivisors = n => {
let rRoot = Math.sqrt(n),
intRoot = Math.floor(rRoot),
blnPerfectSquare = rRoot === intRoot,
lows = range(1, intRoot)
.filter(x => (n % x) === 0);
// for perfect squares, we can drop
// the head of the 'highs' list
return lows.concat(lows
.map(x => n / x)
.reverse()
.slice(blnPerfectSquare | 0)
)
.slice(0, -1); // except n itself
},
// range :: Int -> Int -> [Int]
range = (m, n) => Array.from({
length: (n - m) + 1
}, (_, i) => m + i);
return {
properDivisorsOf1to10: range(1, 10)
.reduce((a, x) => (
a[x.toString()] = properDivisors(x),
a
), {}),
intMaxDivisorsUnder20k: range(1, 20000)
.reduce((a, x) => {
let intDivisors = properDivisors(x)
.length;
return intDivisors >= a.divisors ? {
max: x,
divisors: intDivisors
} : a;
}, {
max: 0,
divisors: 0
})
};
})();</lang>
{{Out}}
<lang JavaScript>{
"properDivisorsOf1to10":{
"1":[], "2":[1], "3":[1], "4":[1, 2], "5":[1],
"6":[1, 2, 3], "7":[1], "8":[1, 2, 4], "9":[1, 3], "10":[1, 2, 5]
},
"intMaxDivisorsUnder20k":{"max":18480, "divisors":79}
}</lang>
=={{header|jq}}==
|