When i was 6th class in primary school, we had just finished the primary maths curriculum and had a few days to spare, so the teacher decided he’d try teach us a bit of algebra.

As a test of our out-of-the-box thinking, he posed this little puzzle:

Given a triangle of balls, where there are x balls on the bottom, x-1 above that, x-2 above that, and so on until there is only one on top, how can you figure out x if you are given the overall total y

O O O O O O O O O O O O O O O x=5, y=15

In other words, if x is 5, then there are 5+4+3+2+1=15 balls in total. Given the number 15, how would you figure out that the original x was 5?

I was a bit of a whiz back then and waded in to the problem. About 5 minutes later, I had an answer – but it was for the other way around – given x, I could figure out y with a simple formula:

y=x((x+1)/2)

So, given x=5, y=5((5+1)/2), y=15.

The teacher was a bit surprised at this. After all, I was about 12 years old at the time.

It didn’t solve the problem as it was asked, though.

All through secondary school, I tried reversing the formula to put it in terms of y instead of x. I didn’t succeed until only recently:

y=x((x+1)/2) 2y=x^{2}+x 2y=(x+1/2)(x+1/2)-1/4<-- this step only occurred to me a few weeks ago2y+1/4=(x+1/2)^{2}(2y+1/4)^{1/2}-1/2=x

How a primary school student was supposed to guess that formula, I have no idea!

To test, we plug in the 15 Y example we had:

(2*15+1.4)^{1/2}-1/2=x 30.25^{1/2}-.5=x 5.5-.5=x x=5

Success, but not pretty!

There /is/ a way of doing it which doesn’t involve quite as much maths.

First, you imagine how the triangle looks:

O O O O O O O O O O O O O O O

Nothing obvious yet… But, shift all the levels over to the left:

O O O O O O O O O O O O O O O

Now, it is obvious that the triangle is half of a square.

It’s not /exactly/ half, but it’s close enough that you can easily figure out x by getting the square root of 2y, then rounding down.

x = Floor[2y^{1/2}]

I am almost certain that that’s the method that my teacher expected us to come up with. Took me 15 minutes to figure out the original formula, and 18 years to notice the much more obvious way to do it.

I had this problem, in another format:

Q1.” Add the numbers from 1 to a 100″… and for those that were going to bite the bullet and spend 10min punching it in on a calculator, Q2. “Add the numbers from 1 to 5000”.

Effectively, it was the same problem… you needed a formula or you were going to get severely lost.

So, we hacked away at many, then a few bright minds, came up with:

If you “doubled” the set to be calculated (so, 1-100, twice), you could quickly figure this out…

Add set one, to set two (reversed)…

So:

1 + 100 = 101

2 + 99 = 101

3 + 98 = 101

…

99 + 2 = 101

100 + 1 = 101

Thus,Y = (X • (X+1))/2

Mentally, I can apply the reverse formula… (I would format this better, but I don’t have a superscript option)

Y • 2 = X{squared} + X

Thus, if the total “balls” was 55…

55 • 2 = X{squared} + X

110 = X{squared} + X

which gets to what you have, X = Floor( SQRT(Y • 2) );

The best part in my case, was the students that figured out what Q2 was “sum(1-1000)”, got to go home early… best 15min of extra sunshine I ever had! 😉