Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,20 @@
function factors(num)
{
var
n_factors = [],
i;
for (i = 1; i <= Math.floor(Math.sqrt(num)); i += 1)
if (num % i === 0)
{
n_factors.push(i);
if (num / i !== i)
n_factors.push(num / i);
}
n_factors.sort(function(a, b){return a - b;}); // numeric sort
return n_factors;
}
factors(45); // [1,3,5,9,15,45]
factors(53); // [1,53]
factors(64); // [1,2,4,8,16,32,64]

View file

@ -0,0 +1,19 @@
// Monadic bind (chain) for lists
function chain(xs, f) {
return [].concat.apply([], xs.map(f));
}
// [m..n]
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
function factors_naive(n) {
return chain( range(1, n), function (x) { // monadic chain/bind
return n % x ? [] : [x]; // monadic fail or inject/return
});
}
factors_naive(6)

View file

@ -0,0 +1 @@
[1, 2, 3, 6]

View file

@ -0,0 +1,59 @@
console.log(
(function (lstTest) {
// INTEGER FACTORS
function integerFactors(n) {
var rRoot = Math.sqrt(n),
intRoot = Math.floor(rRoot),
lows = range(1, intRoot).filter(function (x) {
return (n % x) === 0;
});
// for perfect squares, we can drop the head of the 'highs' list
return lows.concat(lows.map(function (x) {
return n / x;
}).reverse().slice((rRoot === intRoot) | 0));
}
// [m .. n]
function range(m, n) {
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
return m + i;
});
}
/*************************** TESTING *****************************/
// TABULATION OF RESULTS IN SPACED AND ALIGNED COLUMNS
function alignedTable(lstRows, lngPad, fnAligned) {
var lstColWidths = range(0, lstRows.reduce(function (a, x) {
return x.length > a ? x.length : a;
}, 0) - 1).map(function (iCol) {
return lstRows.reduce(function (a, lst) {
var w = lst[iCol] ? lst[iCol].toString().length : 0;
return (w > a) ? w : a;
}, 0);
});
return lstRows.map(function (lstRow) {
return lstRow.map(function (v, i) {
return fnAligned(v, lstColWidths[i] + lngPad);
}).join('')
}).join('\n');
}
function alignRight(n, lngWidth) {
var s = n.toString();
return Array(lngWidth - s.length + 1).join(' ') + s;
}
// TEST
return '\nintegerFactors(n)\n\n' + alignedTable(
lstTest.map(integerFactors).map(function (x, i) {
return [lstTest[i], '-->'].concat(x);
}), 2, alignRight
) + '\n';
})([25, 45, 53, 64, 100, 102, 120, 12345, 32766, 32767])
);

View file

@ -0,0 +1,12 @@
integerFactors(n)
25 --> 1 5 25
45 --> 1 3 5 9 15 45
53 --> 1 53
64 --> 1 2 4 8 16 32 64
100 --> 1 2 4 5 10 20 25 50 100
102 --> 1 2 3 6 17 34 51 102
120 --> 1 2 3 4 5 6 8 10 12 15 20 24 30 40 60 120
12345 --> 1 3 5 15 823 2469 4115 12345
32766 --> 1 2 3 6 43 86 127 129 254 258 381 762 5461 10922 16383 32766
32767 --> 1 7 31 151 217 1057 4681 32767

View file

@ -0,0 +1,73 @@
(function (lstTest) {
'use strict';
// INTEGER FACTORS
// integerFactors :: Int -> [Int]
let integerFactors = (n) => {
let rRoot = Math.sqrt(n),
intRoot = Math.floor(rRoot),
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((rRoot === intRoot) | 0)
);
},
// range :: Int -> Int -> [Int]
range = (m, n) => Array.from({
length: (n - m) + 1
}, (_, i) => m + i);
/*************************** TESTING *****************************/
// TABULATION OF RESULTS IN SPACED AND ALIGNED COLUMNS
let alignedTable = (lstRows, lngPad, fnAligned) => {
var lstColWidths = range(
0, lstRows
.reduce(
(a, x) => (x.length > a ? x.length : a),
0
) - 1
)
.map((iCol) => lstRows
.reduce((a, lst) => {
let w = lst[iCol] ? lst[iCol].toString()
.length : 0;
return (w > a) ? w : a;
}, 0));
return lstRows.map((lstRow) =>
lstRow.map((v, i) => fnAligned(
v, lstColWidths[i] + lngPad
))
.join('')
)
.join('\n');
},
alignRight = (n, lngWidth) => {
let s = n.toString();
return Array(lngWidth - s.length + 1)
.join(' ') + s;
};
// TEST
return '\nintegerFactors(n)\n\n' + alignedTable(lstTest
.map(integerFactors)
.map(
(x, i) => [lstTest[i], '-->'].concat(x)
), 2, alignRight
) + '\n';
})([25, 45, 53, 64, 100, 102, 120, 12345, 32766, 32767]);