Last active
December 8, 2015 18:16
-
-
Save nicolo-ribaudo/a73db425445610cf8380 to your computer and use it in GitHub Desktop.
Pascal's triangle
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
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" |
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
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