Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
61
Task/Amicable-pairs/JavaScript/amicable-pairs-1.js
Normal file
61
Task/Amicable-pairs/JavaScript/amicable-pairs-1.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
(function (max) {
|
||||
|
||||
// Proper divisors
|
||||
function properDivisors(n) {
|
||||
if (n < 2) return [];
|
||||
else {
|
||||
var rRoot = Math.sqrt(n),
|
||||
intRoot = Math.floor(rRoot),
|
||||
|
||||
lows = range(1, intRoot).filter(function (x) {
|
||||
return (n % x) === 0;
|
||||
});
|
||||
|
||||
return lows.concat(lows.slice(1).map(function (x) {
|
||||
return n / x;
|
||||
}).reverse().slice((rRoot === intRoot) | 0));
|
||||
}
|
||||
}
|
||||
|
||||
// [m..n]
|
||||
function range(m, n) {
|
||||
var a = Array(n - m + 1),
|
||||
i = n + 1;
|
||||
while (i--) a[i - 1] = i;
|
||||
return a;
|
||||
}
|
||||
|
||||
// Filter an array of proper divisor sums,
|
||||
// reading the array index as a function of N (N-1)
|
||||
// and the sum of proper divisors as a potential M
|
||||
|
||||
var pairs = range(1, max).map(function (x) {
|
||||
return properDivisors(x).reduce(function (a, d) {
|
||||
return a + d;
|
||||
}, 0)
|
||||
}).reduce(function (a, m, i, lst) {
|
||||
var n = i + 1;
|
||||
|
||||
return (m > n) && lst[m - 1] === n ? a.concat([[n, m]]) : a;
|
||||
}, []);
|
||||
|
||||
// [[a]] -> bool -> s -> s
|
||||
function wikiTable(lstRows, blnHeaderRow, strStyle) {
|
||||
return '{| class="wikitable" ' + (
|
||||
strStyle ? 'style="' + strStyle + '"' : ''
|
||||
) + lstRows.map(function (lstRow, iRow) {
|
||||
var strDelim = ((blnHeaderRow && !iRow) ? '!' : '|');
|
||||
|
||||
return '\n|-\n' + strDelim + ' ' + lstRow.map(function (v) {
|
||||
return typeof v === 'undefined' ? ' ' : v;
|
||||
}).join(' ' + strDelim + strDelim + ' ');
|
||||
}).join('') + '\n|}';
|
||||
}
|
||||
|
||||
return wikiTable(
|
||||
[['N', 'M']].concat(pairs),
|
||||
true,
|
||||
'text-align:center'
|
||||
) + '\n\n' + JSON.stringify(pairs);
|
||||
|
||||
})(20000);
|
||||
2
Task/Amicable-pairs/JavaScript/amicable-pairs-2.js
Normal file
2
Task/Amicable-pairs/JavaScript/amicable-pairs-2.js
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[[220,284],[1184,1210],[2620,2924],[5020,5564],
|
||||
[6232,6368],[10744,10856],[12285,14595],[17296,18416]]
|
||||
61
Task/Amicable-pairs/JavaScript/amicable-pairs-3.js
Normal file
61
Task/Amicable-pairs/JavaScript/amicable-pairs-3.js
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// amicablePairsUpTo :: Int -> [(Int, Int)]
|
||||
const amicablePairsUpTo = n => {
|
||||
const sigma = compose(sum, properDivisors);
|
||||
return enumFromTo(1)(n).flatMap(x => {
|
||||
const y = sigma(x);
|
||||
return x < y && x === sigma(y) ? ([
|
||||
[x, y]
|
||||
]) : [];
|
||||
});
|
||||
};
|
||||
|
||||
// properDivisors :: Int -> [Int]
|
||||
const properDivisors = n => {
|
||||
const
|
||||
rRoot = Math.sqrt(n),
|
||||
intRoot = Math.floor(rRoot),
|
||||
lows = enumFromTo(1)(intRoot)
|
||||
.filter(x => 0 === (n % x));
|
||||
return lows.concat(lows.map(x => n / x)
|
||||
.reverse()
|
||||
.slice((rRoot === intRoot) | 0, -1));
|
||||
};
|
||||
|
||||
|
||||
// TEST -----------------------------------------------
|
||||
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
console.log(unlines(
|
||||
amicablePairsUpTo(20000).map(JSON.stringify)
|
||||
));
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS ----------------------------------
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
x => fs.reduceRight((a, f) => f(a), x);
|
||||
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m => n =>
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
|
||||
// sum :: [Num] -> Num
|
||||
const sum = xs => xs.reduce((a, x) => a + x, 0);
|
||||
|
||||
|
||||
// unlines :: [String] -> String
|
||||
const unlines = xs => xs.join('\n');
|
||||
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue