r/dailyprogrammer • u/Elite6809 1 1 • Mar 16 '14
[17/04/2014] Challenge #153 [Easy] Pascal's Pyramid
(Easy): Pascal's Pyramid
You may have seen Pascal's Triangle before. It has been known about for a long time now and is a very simple concept - it makes several appearances in mathematics, one of which is when you calculate the binomial expansion.
If you've not seen it before, you can calculate it by first putting 1 on the outermost numbers:
1
1 1
1 _ 1
1 _ _ 1
1 _ _ _ 1And then each number within the triangle can be calculated by adding the two numbers above it, to form this:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1It has several interesting properties, however what we're interested in is the 3-dimensional version of this triangle - Pascal's Pyramid.
It works in much the same way - the corner numbers are all 1s. However the edges are all layers of Pascal's triangle, and each inner number is the sum of the 3 numbers above it. Besides that there is nothing else to it.
Here are the first 5 cross-sectional 'layers', top to bottom:
1
1
1 1
1
2 2
1 2 1
1
3 3
3 6 3
1 3 3 1
1
4 4
6 12 6
4 12 12 4
1 4 6 4 1See how the outermost 'rows' or 'edges' of numbers on all of the above are layers of Pascal's Triangle, as we saw above. Therefore, The faces of Pascal's Pyramid, were it a 3D object, would have Pascal's Triangle on them!
Your challenge is, given a number N, to calculate and display the Nth layer of Pascal's Pyramid.
Formal Inputs and Outputs
Input Description
On the console, you will be given a number N where N > 0.
Output Description
You must print out layer N of Pascal's Pyramid. The triangle cross-section must be presented so the point is at the top. Each row shall be separated by newlines, and each number shall be separated by spaces. Spacing is not important but your submission would be even cooler if it were displayed properly. For example, for the 3rd layer, a valid output would be as so:
1
2 2
1 2 1Or, better:
1
2 2
1 2 1Or even:
1
2 2
1 2 1But why you'd do the latter is beyond me.
Sample Inputs & Outputs
Sample Input
6Sample Output
1
5 5
10 20 10
10 30 30 10
5 20 30 20 5
1 5 10 10 5 1Challenge
Challenge Input
14Notes
There are ways to quickly do this that use the Factorial function. Also, look at the pattern the 'rows' make in relation to the leftmost and rightmost number and Pascal's triangle.
Reading material on Pascal's Pyramid can be found here.
Jagged multidimensional arrays will come in handy here.
I'm still trying to gauge relative challenge difficulty, so please excuse me and let me know if this is too challenging for an Easy rating.
4
u/Elite6809 1 1 Mar 16 '14
My ruby solution. I was very tempted to code-golf this one but decided to make it clearer.
4
u/ooesili Mar 17 '14
Oh man, I'm really excited that I got this to work. I created a Haskell program than can print a Pascal's pyramid/triangle in any dimension and size. It differs from the original problem in the fact that it doesn't print a specific layer of the pyramid/triangle, it prints the whole thing.
It's pretty hard to represent a 3-dimensional shape on a terminal, let alone a 4 or 5-dimensional object, so it just prints the internal data structure that I'm using to keep track of the pyramid.
Before we get to the code, I want to take a moment to apologize to any physics-enthusiasts whom I may upset through my misuse of the words "brane" and "string". They were the first names that I thought of and they made enough sense to me.
Now some examples. Here's a 3-dimensional, 4-layer pyramid (like in the original post) so that you can get a grasp of how the output is formatted:
Input:
Output:
Let's switch it up. Here's a 4-dimensional, 3-"layer" pyramid:
Input:
Output:
3
u/ooesili Mar 17 '14
Golf-ish Haskell solution using the trinomial coefficient expansion method.
2
u/dooglehead Mar 17 '14 edited Mar 17 '14
Solution in C#. The solution is longer than it needs to be because it uses the ratio between coeficiants property instead of factorials, which I think would be much faster.
+/u/CompileBot C#
input:
10
u/threeifbywhiskey 0 1 Mar 17 '14
I've taken to playing a very different type of golf wherein I try to use as few alphanumeric characters as possible‒zero, in fact! To that end, here's my solution in Ruby:
And here it is running live on Ideone. There's also a version that takes n on standard input, but I haven't quite figured out how to capture input non-alphanumerically yet, so it cheats a little to get the input. Still, it helps to show that the code is genuinely doing what it's supposed to do.
For a much better glimpse into such a proof, here's a less symbol-heavy rendition. It's essentially /u/Elite6809's solution rewritten to refrain from using the names of any methods or Ruby keywords; this, of course, to facilitate its eventual translation into non-alphanumeric characters.
1
u/starscream92 Mar 21 '14 edited Mar 21 '14
My first submission!
Here's my Java implementation. It doesn't take any shortcuts, instead calculates the whole pyramid up to depth N, as well as the required triangle. Very inefficient, but I reckon nobody would've done the brute force approach.
Code:
Input:
Output:
3
u/toodim Mar 19 '14
Python 3.
Output:
2
u/foviendsciuf Mar 17 '14
a python approach
2
u/DMzda Mar 17 '14
Python 2.7 solution.
I decided to try to use generators for the first time, but I had trouble figuring out how to implement them, so I started with normal functions then converted them to generators. I also had trouble implementing the factorial solution shown on the wiki page, so I implemented this method that depends on calculating Pascal's triangle first to the same number of layers needed in the pyramid. It's not the greatest solution, but it works:
I'm not sure if the pyramid generator is any more efficient than the normal function. I think this because of my use of
islicefunction to get the final line of the triangle. I could use any tips on how to see how efficient a generator function is compared to a normal one.Here is the output for the challenge input:
2
u/Ekrazit Mar 17 '14
my quick java solution:
2
u/Edward_H Mar 17 '14
COBOL:
2
u/[deleted] Mar 17 '14
Python 3
2
u/Sakuya_Lv9 Mar 18 '14 edited Mar 18 '14
Ruby solution. Focuses on reducing the time complexity.
+/u/CompileBot Ruby
3
u/badgers_uk Mar 17 '14 edited Mar 17 '14
Python 3. It's my first post on here but I've enjoyed going through some of the older challenges :) Only recently started learning so any feedback is very welcome. I think readability is a problem - I spent a while tinkering to get it to work...
Edit: improved the formatting by creating a function to find largest number, centre each number. then centre each line. Edit #100: Ok I should leave this alone now.
1
u/dongas420 Mar 17 '14
Perl:
1
u/M4a1x Mar 17 '14
Python3.3 (yet another one) by trying to use this Wikipedia approach Any Feedback is welcome!
1
u/dooglehead Mar 17 '14
This is my second solution to this challenge. This time, I wrote it in C. It is much shorter than my C# solution and probably faster.
1
u/lukz 2 0 Mar 18 '14
BASIC
I use the most straightforward solution and I generate layer by layer from 1 to N. Also, this is the first time I read about Pascal's pyramid.
Sample session
1
u/Alborak Mar 23 '14
Here's my very C-style Python 2.7 solution. Pyramid calculation is from wikipedia
1
u/overnight_mail Mar 24 '14
Just a quick java solution without using factorial. It's missing some input verification, but for inputs of N<2 you could just print the pyramid.
1
u/Gflat Mar 26 '14 edited Mar 26 '14
Learning F#. Here's a super-naive solution that does not use factorials.
+/u/CompileBot F#
Input:
[edit: CompileBot doesn't like tabs]
1
u/DiabeetusMan Mar 27 '14 edited Mar 27 '14
My attempt in Python 33. It's definitely not the most efficient, but it does make the output nice and pretty.
Input
Output
Input
Output
1
u/sygede Apr 21 '14 edited Apr 24 '14
just start learning c++, used recursion, new, and return by pointer. but I have one question: can someone point to me where should I insert delete pair with new?
thanks!!
1
u/pteek Mar 17 '14
My freind wrote this about 6months ago. Neat solution. C.
0
u/thinksInCode Mar 18 '14
Java:
0
u/[deleted] Mar 21 '14
[deleted] — view deleted comment