Skip to content

Instantly share code, notes, and snippets.

@ecancino
Created May 23, 2017 17:26
Show Gist options
  • Select an option

  • Save ecancino/c92b6e67ae95391c77ec27d5829c2fa0 to your computer and use it in GitHub Desktop.

Select an option

Save ecancino/c92b6e67ae95391c77ec27d5829c2fa0 to your computer and use it in GitHub Desktop.
pascal
'use strict';
const { compose, curry, times, max, add, map, join, memoize } = require('ramda')
const toFixed = curry((p, n) => Number(n.toFixed(p)))
const format = compose(join('\n'), map(join(', ')), map(map(toFixed(3))))
const pascal = memoize((n, s = 1) => {
const result = [[s]]
times((n) => {
const row = n + 1
result[row] = []
times((col) => {
result[row][col] = add(
max(0, result[row - 1][col]),
max(0, result[row - 1][col - 1])
)
}, row + 1)
}, n)
return result
})
const Φ = 1.618033988749894848204586834
const C = toFixed(10, Φ)
console.log(format(pascal(10, C)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment