So the other day in the shower I began to think,  If I had (t) number of cans and I wanted to place them in a triangular stack how could I figure out how many cans need to be on the bottom row (r) and how many rows would it take to get to the top of the stack? Before you disregard me as some sort of nut let me provide some back story. (Although if you think I'm a bit strange you are on the right track). It's common knowledge that developers (myself included) run on energy drinks.

Unfortunately I do not enjoy the taste of most energy drinks, with the exception of Rockstar Pomegranate. This stuff, I could drink by the case (if my heart could handle that much caffeine) unfortunately they do not sell it in stores anymore. Talking about it with a fellow co-worker a few weeks ago, she found out that you could order cases of Rockstar Pomegranate directly from the rockstar website. with out delay, we split a case of 24. Flash forward a few weeks, and the wonderful case of developer-fueling goodness arrives and we began to drink it. So I began to think, what can we do with the empty cans. We cant just throw them out, each can represents a badge of hard work. We need to display them, somewhere.

Back to the Shower.. I'm in my shower when the idea comes to me, why not build a pyramidal stack of cans between our desks? This is where my mind starts going down it's nerdy path.  I'm going to best recreate my thought pattern, so you can get some insight into how strange my mind works.

" Ohhh.. we could build a pyramid of cans! there is plenty of room against the wall between our desks. Hmm... I wonder how big of a pyramid we can build, how many cans in the case?.. 24. well, the top level would be one can, the second row would be two cans. I wonder if there is a formula for figuring out the total number of items in a pyramid stack? So one can, obviously is one. three cans equals 2 rows, a row of one can, and a second row of two cans. Three rows equals 6 cans, 4 rows equals 10 cans.  What would the formula be? Well if  t equals the total number of cans, and r equals the number of rows how could I find the value of r using the value of t. For t = 1, r = 1. for t = 3, r = 2 and for t = 6, r =3. So then r = t divided by?, well for one, it's divided by one and for 6 it's divided by 2, so for 3 it's divided by 1.5? that doesn't seem right. there is deffiently a pattern, which means there is a formula. I guess I could look it up on google. Screw that. I'm smart, well I'm not google smart, but smart enough where I can figure it out. I bet I could program the solution.  did I already shampoo my hair?"

So you get the idea.  I left the house that day, stuck on figuring out this problem. I could clearly see that there was a pattern, I just had to figure out the formula that create that pattern. Here is the process I went about solving it, I received some help from co-workers and the internet. First thing I did was write out the permutations I knew the answers too.

Total Cans (t) Number of Rows (r) Number of cans in bottom row
1 1 1
2 N/A N/A
3 2 2
4 N/A N/A
5 N/A N/A
6 3 3
10 4 4
15 5 5

Right away I could see that the number or rows was equal to the number of cans in the bottom row.  So r = number of rows AND number or cans in bottom row. I could also see that only certain numbers could be used to produce a pyramid. 1,3,6,10,15,... There was a pattern to them, 1 (+2 = ) 3 (+3 = ) 6 (+4 = ) 10 (+ 5 = ) 15. I was not sure if that would be helpful, but I kept note.  As I worked, my mind kept working on this problem, by the end of the day I had my first formula: r = (t+x)/y where: r = number of rows/cans on bottom row t = total number of cans x = (some value, or 0) y = some other value. Concrete I know. but I was on to something, the formula worked for my examples

r t x y
1 1 1 1
2 3 1 2
3 6 0 2

It seemed like y = 2, and x= well that was less concreate. Eventually I realized this forumal, although working for my examples, was not correct. The next day during lunch I talked with some of my co-workers about this problem I was working on. Lucky for us, we have a black board in our lunch room, and as I rambled to them I wrote down my examples. With there help, we came up with this formula: t = (r(r+1)) / 2; Which I took a step further to : t = (r² + r) / 2. That's great and all if I was trying to take the number of rows and solve for the total cans (it's an arithmetic sequence). But I wanted to do the opposite. Well, that should be as simple as taking the formula and solving for r.  It's harded then it looks. Let me show you a few of the steps I went through. t = (r²+ r) /2 2t = r² + r 2t - r = r² √(2t - r) = r OK, great I've solved for r. the problem is that r is in the solution. That's like providing a word in its meaning, not helpful. I was stuck  I could not get past this part on my own, I'm just not that good at math.  Enter Wolframalpha. Wolframalpha is this great statistical/mathmatical search engine. It's one of the closets things we have to skynet. So first I fed it my t formula and it provided me with all these great graphs and numbers. (Here is a link) Now how was I going to get help solving for r ?  Well I under estimated how smart wolfram alpha is.  My next query was simply "t = ((r^2) + r)/2 solve for r"  and here was what it came up with So it looks like I had found my answer.  the formula I was looking for was:

r = 1/2 (√(8 t+1)-1)

I doubt I would have been able to figure that solution out on my own.  So I took our number of cans 24, and plugged it into that formula. the result is 6 (well it's not, if t = 21, then it would be 6, 24 does not work for a pyramid). Now i know we'll be able to build a pyramid of cans 6 rows high). wouldn't it have just been easier to start with one can and work my way down, until I ran out of cans. Well, yes it would have been extremely easier. But I am a developer/nerd. My mind thrives on problem solving. It's fun, at least I think so. So next time you are in the shower and you start to think about a problem (work safe problems). I suggest roll with it, you might have fun and you could learn something new.