In other words, if you are given a number k, you need to solve
n * (n+1)/2 <= k
n is the number of elements you would have at the base of your pyramid.
Main Topics
Browse All TopicsSay I have a set number of stackable objects like blocks.
I need an algorithm to basically build a pyramid of objects as tall as possible with the given number of stackable objects.
For example, if I have 10 blocks, they'd stack something like this:
O
OO
OOO
OOOO
The final layer (at bottom) must always have the most items of any of the previous layers.
C or pseudocode would be fine. Thanks in advance!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
@sunnycoder:
Uhm, he cannot start from the top, because for n=3, for example, he will have the base with only 1 element,which contradics the basic requirement.
@ozo:
Your alghorithm is good for calculating the base, but it wrong for calculating stack hight.
For example, for n=11, base has ceil(4,21)=5, which is good, but height is floor(4,21)=4, which is wrong, because a stack with 11 elements will be like this:
oo
oooo
ooooo
therefore height is only 3.
I will supply you with a pseudo-code algorithm later today (in 7-8 hours)
In my alghoritm, I will use ozo's formula for calculating the base dimension, and I'll give him full credit for his formula.
I will build your stack in a square matrix with ceil(sqrt(8*n + 1)-1)/2) lines and colums.
I will initialize all matrix elements with 0;
Knowing the base dimension, I will build the stack by writing 1 until I finish all the given elements.
I will build the stack by using line_dim to know the current line lenght (starting from known base dimension and decreasing by 1 as I go up).
The output will be a matrix looking like that:
1111
1110
1100
1000
for n=10
and looking like that:
11111
11110
11000
00000
00000
for n=11
where 1 represents the stack elements.
I know that your stack will be upside down, but you can correct that by the way you display it. I made it like this to keep a clean alghoritm.
BEGIN algorithm;
read n;
initialise dim with ceil(sqrt(8*n + 1)-1)/2);
initialize a matrix will all elements 0;
initialize counter_elements with n;
initialize i with 0;
initialize line_dim with dim;
while counter_elements>0
begin
i++;
For j=1 to line_dim
begin
a[i,j]=1;
line_dim--;
end;
end;
display matrix a; {careful that your stack is stored upside down in the matrix}
END algorithm.
> Fitting in extra elements is no sweat ... add them all to the base level or one by one from base level upwards (you would need to add 2 to base in worst case)
Well, these two methods of flling in extra are comprised by my codes in #23411668 and #23411861, but of course one has many choices inbetween. In fact, if you are left with k objects after building a maximal perfect pyramid, you have p(k) options to add these without breaking the stacking property, wher p(k) denotes the number of partitions of k; "one by one from base level upwards" corresponds to the partition "1+1+...+1", "add them all to the base" corresponds to "k".
ozo:
I wasn't careful that he wants the highest stack possible, and I assumed that he wants "the most stable" stack, starting from base, with minimum elements on the base.
Indeed, your formula for height is good too.
I apologize :(
sunnycoder:
You just supplied the idea for the quickest algorithm.
Make the perfect stack, and distribute the leftovers one by one, from base up. This actually guarantees the highest stack.
1. Calculate the biggest perfect stack with your number of elements.
read n;
i=1;
while i*(i+1)/2 <=n
m=i*(i+1)/2;
i++
end while;
2. At this point, n-m will be the leftovers to distribute, i-1 will be the base of the perfect stack. I will assume you want your stack stored (and not just displayed), and I will store your stack in a matrix, just as in my first algorithm. It will be a (i-1) X (i-1) matrix (i calculated above) if your n is the sum of first i numbers, and will be a i X i matrix if there are leftovers to complete. The matrix will have 1 where you have a stack element. (careful that the stack will be upside down in the matrix).
3. Treating the lucky case when n is a sum of first i numbers (perfect stack)
if m==n
for j=1 to i-1
for k=1 to i-1
a(j,k)=1
end algorithm
4. Building perfect stack and completing the stack
for j=1 to i-1
for k=1 to i-1
a(j,k)=1
k=1;
while m<n
a(k,i)=1;
k++;
end while;
5. You will have your stack in the a matrix like this:
for n=10
1111
1110
1100
1000
for n=18
111111
111110
111100
110000
100000
And so on.
This is the quickest and the most "clean" algorithm.
Of course I give a lot of credit to sunnycoder for the idea (in his second post he said that the base will have n elements - well, it will have n elements *only* if your number of elements builds a perfect stack.) Also, I must say that you will never have to add 2 elements to the base, not even in the worst case.
Apologize again for not paying fully attention to your requirements, and apologize to ozo, because his formulas were both good.
One thing remains to be proven, that this guarantees the highest stack.
Well, highest stack is guaranteed, because the perfect stack is the highest stack possible with a given number of elements, and the leftovers are too few to build a higher perfect stack. So if you complete with all the leftovers by 1 per level starting from the base and going up, you are sure you have the higher stack, with the least number of elements possible on the base.
Ignore my first algorithm, it doesn't build you the highest stack. It builds you the lowest stack with the least number of elements on the base.
It may be fastest to put all the left overs on the bottom,
but the structure implied by by the formulas in http:#a23408553 can make it easier to go from an n stack to an n+1 stack
Business Accounts
Answer for Membership
by: sunnycoderPosted on 2009-01-18 at 22:09:19ID: 23408458
start with top ... one object here
work downwards +1 at every level.
Essentially sum of first n natural numbers < given number