Created
February 24, 2020 04:26
-
-
Save kylehovey/c019e6f347201a5fc81db5087ca238b5 to your computer and use it in GitHub Desktop.
Scratchpad for problem 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fact = n => n <= 0 ? 1 : n * fact(n-1); | |
const choose = (n, k) => fact(n) / (fact(n - k) * fact(k)); | |
const mapper = predicate => n => predicate(n) ? 1 : 0; | |
const [isEven, isOdd] = [0,1].map(k => mapper(n => n % 2 === k)); | |
const iverson = k => mapper(n => n === k); | |
const coverNegative = f => n => n < 0 ? 0 : f(n); | |
const conjugatePair = (a, b) => [a+b, a-b]; | |
const round = fn => n => Math.round(fn(n)); | |
const range = n => Array(n).fill().map((_, i) => i); | |
const reversed = xs => [...xs].reverse(); | |
const zip = (...xss) => ( | |
len => range(len).map( | |
idx => xss.reduce( | |
(acc, xs) => [...acc, xs[idx]], | |
[] | |
) | |
) | |
)(Math.min(...xss.map(({ length }) => length))); | |
const convolve = (A, B) => zip(A, reversed(B)).reduce( | |
(acc, [a, b]) => acc + a*b, | |
0, | |
); | |
const b_ = n => n === 0 | |
? 1 | |
: ( | |
last => convolve(last, last) | |
)(range(n).map(b_)); | |
const soln = n => ( 1 / (n + 1)) * choose(2*n, n); | |
console.log( | |
range(10).map(b_), | |
range(10).map(soln), | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment