Skip to content

Instantly share code, notes, and snippets.

@nicolo-ribaudo
Last active December 8, 2015 18:16
Show Gist options
  • Save nicolo-ribaudo/a73db425445610cf8380 to your computer and use it in GitHub Desktop.
Save nicolo-ribaudo/a73db425445610cf8380 to your computer and use it in GitHub Desktop.
Pascal's triangle
import { pascal, binomePower } from "./math.js";
console.log(pascal(10)); // [1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1]
console.log(binomePower(3)); // "x^3 + 3x^2y + 3xy^2 + y^3"
export function pascal(l) {
return l > 0 ? [0].concat(pascal(l - 1)).map((n, i, p) => n + (p[i + 1] || 0)) : [1];
}
export function binomePower(e) {
return pascal(e).map(
(c, i) => [
c > 1 && c,
e - i > 0 && "x",
e - i > 1 && "^" + (e - i),
i > 0 && "y",
i > 1 && "^" + i,
].map(x => x || "").join("")
).join(" + ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment