Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1 @@
|
|||
Number.MAX_SAFE_INTEGER
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
(function (lstFactors, intExponent) {
|
||||
|
||||
// [n] -> n -> n
|
||||
function sumMultiplesBelow(lstIntegers, limit) {
|
||||
return range(1, limit - 1).filter(function (x) {
|
||||
return isMultiple(lstIntegers, x);
|
||||
}).reduce(function (a, n) {
|
||||
return a + n;
|
||||
}, 0)
|
||||
}
|
||||
|
||||
// [n] -> n -> bool
|
||||
function isMultiple(lst, n) {
|
||||
var i = lng;
|
||||
while (i--)
|
||||
if (n % (lst[i]) === 0) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
// [m..n]
|
||||
function range(m, n) {
|
||||
var a = Array(n - m + 1),
|
||||
i = n + 1;
|
||||
while (i--) a[i - 1] = i;
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
/* TESTING */
|
||||
|
||||
// [[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|}';
|
||||
}
|
||||
|
||||
var lng = lstFactors.length,
|
||||
lstSorted = lstFactors.slice(0).sort();
|
||||
|
||||
var lstTable = [['Below', 'Sum']].concat(
|
||||
range(1, intExponent).map(function (x) {
|
||||
var pwr = Math.pow(10, x);
|
||||
|
||||
return ['10^' + x, sumMultiplesBelow(lstSorted, pwr)];
|
||||
})
|
||||
);
|
||||
|
||||
return 'For ' + JSON.stringify(lstFactors) + ':\n\n' +
|
||||
wikiTable(lstTable, true) + '\n\n' +
|
||||
JSON.stringify(lstTable);
|
||||
|
||||
})([3, 5], 8);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
[["Below","Sum"],["10^1",23],["10^2",2318],["10^3",233168],
|
||||
["10^4",23331668],["10^5",2333316668],["10^6",233333166668],
|
||||
["10^7",23333331666668],["10^8",2333333316666668]]
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
function sm35(n){
|
||||
var s=0, inc=[3,2,1,3,1,2,3]
|
||||
for (var j=6, i=0; i<n; j+=j==6?-j:1, i+=inc[j]) s+=i
|
||||
return s
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
function sm35(n){
|
||||
return tri(n,3) + tri(n,5) - tri(n,15)
|
||||
function tri(n, f) {
|
||||
n = Math.floor((n-1) / f)
|
||||
return f * n * (n+1) / 2
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
for (var i=1, n=10; i<9; n*=10, i+=1) {
|
||||
document.write(10, '<sup>', i, '</sup> ', sm35(n), '<br>')
|
||||
}
|
||||
|
|
@ -0,0 +1,141 @@
|
|||
(() => {
|
||||
|
||||
// sum35 :: Int -> Int
|
||||
const sum35 = n => {
|
||||
// The sum of all positive multiples of
|
||||
// 3 or 5 below n.
|
||||
const f = sumMults(n);
|
||||
return f(3) + f(5) - f(15);
|
||||
};
|
||||
|
||||
|
||||
// sumMults :: Int -> Int -> Int
|
||||
const sumMults = n =>
|
||||
// Area under straight line
|
||||
// between first multiple and last.
|
||||
factor => {
|
||||
const n1 = quot(n - 1)(factor);
|
||||
return quot(factor * n1 * (n1 + 1))(2);
|
||||
};
|
||||
|
||||
// ------------------------- TEST --------------------------
|
||||
|
||||
// main :: IO ()
|
||||
const main = () =>
|
||||
fTable('Sums for n = 10^1 thru 10^8:')(str)(str)(
|
||||
sum35
|
||||
)(
|
||||
enumFromTo(1)(8)
|
||||
.map(n => Math.pow(10, n))
|
||||
);
|
||||
|
||||
|
||||
// ------------------------ GENERIC ------------------------
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m =>
|
||||
n => !isNaN(m) ? (
|
||||
Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i)
|
||||
) : enumFromTo_(m)(n);
|
||||
|
||||
|
||||
// quot :: Int -> Int -> Int
|
||||
const quot = n =>
|
||||
m => Math.floor(n / m);
|
||||
|
||||
|
||||
// ------------------------ DISPLAY ------------------------
|
||||
|
||||
// compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
|
||||
const compose = (...fs) =>
|
||||
// A function defined by the right-to-left
|
||||
// composition of all the functions in fs.
|
||||
fs.reduce(
|
||||
(f, g) => x => f(g(x)),
|
||||
x => x
|
||||
);
|
||||
|
||||
|
||||
// fTable :: String -> (a -> String) -> (b -> String)
|
||||
// -> (a -> b) -> [a] -> String
|
||||
const fTable = s =>
|
||||
// Heading -> x display function ->
|
||||
// fx display function ->
|
||||
// f -> values -> tabular string
|
||||
xShow => fxShow => f => xs => {
|
||||
const
|
||||
ys = xs.map(xShow),
|
||||
w = Math.max(...ys.map(length));
|
||||
return s + '\n' + zipWith(
|
||||
a => b => a.padStart(w, ' ') + ' -> ' + b
|
||||
)(ys)(
|
||||
xs.map(x => fxShow(f(x)))
|
||||
).join('\n');
|
||||
};
|
||||
|
||||
|
||||
// length :: [a] -> Int
|
||||
const length = xs =>
|
||||
// Returns Infinity over objects without finite
|
||||
// length. This enables zip and zipWith to choose
|
||||
// the shorter argument when one is non-finite,
|
||||
// like cycle, repeat etc
|
||||
'GeneratorFunction' !== xs.constructor.constructor.name ? (
|
||||
xs.length
|
||||
) : Infinity;
|
||||
|
||||
|
||||
// list :: StringOrArrayLike b => b -> [a]
|
||||
const list = xs =>
|
||||
// xs itself, if it is an Array,
|
||||
// or an Array derived from xs.
|
||||
Array.isArray(xs) ? (
|
||||
xs
|
||||
) : Array.from(xs);
|
||||
|
||||
|
||||
// str :: a -> String
|
||||
const str = x =>
|
||||
Array.isArray(x) && x.every(
|
||||
v => ('string' === typeof v) && (1 === v.length)
|
||||
) ? (
|
||||
x.join('')
|
||||
) : x.toString();
|
||||
|
||||
|
||||
// take :: Int -> [a] -> [a]
|
||||
// take :: Int -> String -> String
|
||||
const take = n =>
|
||||
// The first n elements of a list,
|
||||
// string of characters, or stream.
|
||||
xs => 'GeneratorFunction' !== xs
|
||||
.constructor.constructor.name ? (
|
||||
xs.slice(0, n)
|
||||
) : [].concat.apply([], Array.from({
|
||||
length: n
|
||||
}, () => {
|
||||
const x = xs.next();
|
||||
return x.done ? [] : [x.value];
|
||||
}));
|
||||
|
||||
|
||||
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
|
||||
const zipWith = f =>
|
||||
// Use of `take` and `length` here allows zipping with non-finite lists
|
||||
// i.e. generators like cycle, repeat, iterate.
|
||||
xs => ys => {
|
||||
const n = Math.min(length(xs), length(ys));
|
||||
return (([as, bs]) => Array.from({
|
||||
length: n
|
||||
}, (_, i) => f(as[i])(
|
||||
bs[i]
|
||||
)))([xs, ys].map(
|
||||
compose(take(n), list)
|
||||
));
|
||||
};
|
||||
|
||||
// ---
|
||||
return main();
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue