June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -1,58 +1,104 @@
(() => {
const dctMemo = {};
// Length only of hailstone sequence
// hailstones :: Int -> [Int]
const hailstones = x => {
const collatz = memoized(n =>
even(n) ? div(n, 2) : (3 * n) + 1);
return reverse(until(
xs => xs[0] === 1,
xs => cons(collatz(xs[0]), xs), [x]
));
};
// collatzLength :: Int -> Int
const collatzLength = n => {
let i = 1;
let a = n;
let lng;
const collatzLength = n =>
until(
xi => xi[0] === 1,
([x, i]) => [(x % 2 ? 3 * x + 1 : x / 2), i + 1], //
[n, 1]
)[1];
while (a !== 1) {
lng = dctMemo[a];
if ('u' === (typeof lng)[0]) {
a = (a % 2 ? 3 * a + 1 : a / 2);
i++;
} else return lng + i - 1;
}
return i;
// 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);
};
// cons :: a -> [a] -> [a]
const cons = (x, xs) => [x].concat(xs);
// div :: Int -> Int -> Int
const div = (x, y) => Math.floor(x / y);
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
// even :: Int -> Bool
const even = n => n % 2 === 0;
// fst :: (a, b) -> a
const fst = pair => pair.length === 2 ? pair[0] : undefined;
// map :: (a -> b) -> [a] -> [b]
const map = (f, xs) => xs.map(f);
// maximumBy :: (a -> a -> Ordering) -> [a] -> a
const maximumBy = (f, xs) =>
xs.length > 0 ? (
xs.slice(1)
.reduce((a, x) => f(x, a) > 0 ? x : a, xs[0])
) : undefined;
// memoized :: (a -> b) -> (a -> b)
const memoized = f => {
const dctMemo = {};
return x => {
const v = dctMemo[x];
return v !== undefined ? v : (dctMemo[x] = f(x));
};
};
// range :: Int -> Int -> Maybe Int -> [Int]
const range = (m, n, delta) => {
const blnUp = n > m,
d = blnUp ? (delta || 1) : -(delta || 1),
lng = Math.abs(Math.floor((blnUp ? n - m : m - n) / d) + 1),
a = Array(lng);
let i = lng;
// reverse :: [a] -> [a]
const reverse = xs =>
xs.slice(0)
.reverse();
while (i--) a[i] = (d * i) + m;
return a;
// unlines :: [String] -> String
const unlines = xs => xs.join('\n');
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
let v = x;
while (!p(v)) v = f(v);
return v;
};
// longestBelow :: Int -> {Number::Int, Length:Int}
const longestBelow = n =>
range(1, n)
.reduce(
(a, x) => {
const lng = dctMemo[x] || (dctMemo[x] = collatzLength(x));
// MAIN ------------------------------------------------------------------
const
// ceiling :: Int
ceiling = 100000,
return lng > a.l ? {
n: x,
l: lng
} : a
}, {
n: 0,
l: 0
}
// (maxLen, maxNum) :: (Int, Int)
[maxLen, maxNum] =
maximumBy(
comparing(fst),
map(i => [collatzLength(i), i], enumFromTo(1, ceiling))
);
// TEST
// show :: a -> String
const show = x => JSON.stringify(x, null, 2);
return show(
[100000, 1000000, 10000000].map(longestBelow)
);
return unlines([
'Collatz sequence for 27: ',
`${hailstones(27)}`,
'',
`The number ${maxNum} has the longest hailstone sequence`,
`for any starting number under ${ceiling}.`,
'',
`The length of that sequence is ${maxLen}.`
]);
})();