r/programmingchallenges • u/okmkz • Apr 25 '11
Challenge: Compute the nth row of Pascal's triangle
Write a function in the language of your choosing that accepts a non-negative integer n and returns a string containing the nth row of Pascal's Triangle.
EDIT: There really is no reason the function has to return a string, you could just as easily print to stdout.
Example:
pascal(4)
Returns:
"1 4 6 4 1"
Bonus: If you'd prefer, compute the nth layer of Pascal's Pyramid and return an array of strings as follows:
EDIT: See note above regarding return type.
pascal3(3)
Returns:
{"1",
"3 3",
"3 6 3",
"1 3 3 1"}
7
Upvotes
2
u/DoubleFelix Apr 25 '11
This is really more of a math problem, I think.
2
u/[deleted] Apr 26 '11
Challenge accepted. This looked like so much fun, I joined reddit. https://gist.github.com/943101
2
u/Octember Apr 26 '11
I refuse to follow your return type, forgive me.
1
u/[deleted] Apr 25 '11
I like the idea, but your output for the bonus is wrong...
1
u/kageurufu Apr 26 '11
i was close, writing on jsfiddle, but didnt save at all
i wrote an infinite loop on accident, crashed the page, and lost it T_T
1
u/robinhouston Sep 09 '11
Haskell:
1
u/zck May 02 '11
Arc:
1
u/mmhrar Sep 09 '11
C++ could probably be cleaner