## a triangle maths formula

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=x2+x
2y=(x+1/2)(x+1/2)-1/4 <-- this step only occurred to me a few weeks ago
2y+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.251/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[2y1/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.