2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
46
Task/Amicable-pairs/JavaScript/amicable-pairs-3.js
Normal file
46
Task/Amicable-pairs/JavaScript/amicable-pairs-3.js
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
(max => {
|
||||
|
||||
// amicablePairsUpTo :: Int -> [(Int, Int)]
|
||||
let amicablePairsUpTo = max =>
|
||||
range(1, max)
|
||||
.map(x => properDivisors(x)
|
||||
.reduce((a, b) => a + b, 0))
|
||||
.reduce((a, m, i, lst) => {
|
||||
let n = i + 1;
|
||||
|
||||
return (m > n) && lst[m - 1] === n ?
|
||||
a.concat([[n, m]]) : a;
|
||||
}, []),
|
||||
|
||||
|
||||
// properDivisors :: Int -> [Int]
|
||||
properDivisors = n => {
|
||||
if (n < 2) return [];
|
||||
else {
|
||||
let rRoot = Math.sqrt(n),
|
||||
intRoot = Math.floor(rRoot),
|
||||
blnPerfectSquare = rRoot === intRoot,
|
||||
|
||||
lows = range(1, intRoot)
|
||||
.filter(x => (n % x) === 0);
|
||||
|
||||
return lows.concat(lows.slice(1)
|
||||
.map(x => n / x)
|
||||
.reverse()
|
||||
.slice(blnPerfectSquare | 0));
|
||||
}
|
||||
},
|
||||
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
range = (m, n, step) => {
|
||||
let d = (step || 1) * (n >= m ? 1 : -1);
|
||||
|
||||
return Array.from({
|
||||
length: Math.floor((n - m) / d) + 1
|
||||
}, (_, i) => m + (i * d));
|
||||
}
|
||||
|
||||
|
||||
return amicablePairsUpTo(max);
|
||||
|
||||
})(20000);
|
||||
2
Task/Amicable-pairs/JavaScript/amicable-pairs-4.js
Normal file
2
Task/Amicable-pairs/JavaScript/amicable-pairs-4.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[[220, 284], [1184, 1210], [2620, 2924], [5020, 5564],
|
||||
[6232, 6368], [10744, 10856], [12285, 14595], [17296, 18416]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue