Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
69
Task/Proper-divisors/JavaScript/proper-divisors-1.js
Normal file
69
Task/Proper-divisors/JavaScript/proper-divisors-1.js
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
(function () {
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
var tblOneToTen = [
|
||||
['Number', 'Proper Divisors', 'Count']
|
||||
].concat(range(1, 10).map(function (x) {
|
||||
var ds = properDivisors(x);
|
||||
|
||||
return [x, ds.join(', '), ds.length];
|
||||
})),
|
||||
|
||||
dctMostBelow20k = range(1, 20000).reduce(function (a, x) {
|
||||
var lng = properDivisors(x).length;
|
||||
|
||||
return lng > a.divisorCount ? {
|
||||
n: x,
|
||||
divisorCount: lng
|
||||
} : a;
|
||||
}, {
|
||||
n: 0,
|
||||
divisorCount: 0
|
||||
});
|
||||
|
||||
|
||||
// [[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(
|
||||
tblOneToTen,
|
||||
true
|
||||
) + '\n\nMost proper divisors below 20,000:\n\n ' + JSON.stringify(
|
||||
dctMostBelow20k
|
||||
);
|
||||
|
||||
})();
|
||||
110
Task/Proper-divisors/JavaScript/proper-divisors-2.js
Normal file
110
Task/Proper-divisors/JavaScript/proper-divisors-2.js
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
(() => {
|
||||
'use strict';
|
||||
|
||||
// properDivisors :: Int -> [Int]
|
||||
const properDivisors = n => {
|
||||
// The integer divisors of n, excluding n itself.
|
||||
const
|
||||
rRoot = Math.sqrt(n),
|
||||
intRoot = Math.floor(rRoot),
|
||||
blnPerfectSquare = rRoot === intRoot,
|
||||
lows = enumFromTo(1)(intRoot)
|
||||
.filter(x => 0 === (n % x));
|
||||
|
||||
// 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
|
||||
};
|
||||
|
||||
|
||||
// ------------------------TESTS-----------------------
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
console.log([
|
||||
fTable('Proper divisors of [1..10]:')(str)(
|
||||
JSON.stringify
|
||||
)(properDivisors)(enumFromTo(1)(10)),
|
||||
'',
|
||||
'Example of maximum divisor count in the range [1..20000]:',
|
||||
' ' + maximumBy(comparing(snd))(
|
||||
enumFromTo(1)(20000).map(
|
||||
n => [n, properDivisors(n).length]
|
||||
)
|
||||
).join(' has ') + ' proper divisors.'
|
||||
].join('\n'));
|
||||
|
||||
|
||||
// -----------------GENERIC FUNCTIONS------------------
|
||||
|
||||
// comparing :: (a -> b) -> (a -> a -> Ordering)
|
||||
const comparing = f =>
|
||||
x => y => {
|
||||
const
|
||||
a = f(x),
|
||||
b = f(y);
|
||||
return a < b ? -1 : (a > b ? 1 : 0);
|
||||
};
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m => n =>
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
// fTable :: String -> (a -> String) -> (b -> String)
|
||||
// -> (a -> b) -> [a] -> String
|
||||
const fTable = s => xShow => fxShow => f => xs => {
|
||||
// Heading -> x display function ->
|
||||
// fx display function ->
|
||||
// f -> values -> tabular string
|
||||
const
|
||||
ys = xs.map(xShow),
|
||||
w = Math.max(...ys.map(x => x.length));
|
||||
return s + '\n' + zipWith(
|
||||
a => b => a.padStart(w, ' ') + ' -> ' + b
|
||||
)(ys)(
|
||||
xs.map(x => fxShow(f(x)))
|
||||
).join('\n');
|
||||
};
|
||||
|
||||
// maximumBy :: (a -> a -> Ordering) -> [a] -> a
|
||||
const maximumBy = f => xs =>
|
||||
0 < xs.length ? (
|
||||
xs.slice(1)
|
||||
.reduce((a, x) => 0 < f(x)(a) ? x : a, xs[0])
|
||||
) : undefined;
|
||||
|
||||
// snd :: (a, b) -> b
|
||||
const snd = tpl => tpl[1];
|
||||
|
||||
// str :: a -> String
|
||||
const str = x => x.toString();
|
||||
|
||||
// until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
const until = p => f => x => {
|
||||
let v = x;
|
||||
while (!p(v)) v = f(v);
|
||||
return v;
|
||||
};
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = f => xs => ys => {
|
||||
const
|
||||
lng = Math.min(xs.length, xs.length),
|
||||
as = xs.slice(0, lng),
|
||||
bs = ys.slice(0, lng);
|
||||
return Array.from({
|
||||
length: lng
|
||||
}, (_, i) => f(as[i])(
|
||||
bs[i]
|
||||
));
|
||||
};
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue