# Sunday Times Teaser 3044 – Building Blocks

*by Colin Vout*

#### Published Sunday January 24 2021 (link)

The kids had used all the blocks each of them owned (fewer than 100 each) to build triangular peaks — one block on the top row, two on the next row, and so on.

“My red one’s nicer!” “My blue one’s taller!”

“Why don’t you both work together to make one bigger still?” I said. I could see they could use all these blocks to make another triangle.

This kept them quiet until I heard, “I bet Dad could buy some yellow blocks to build a triangle bigger than either of ours was, or a red and yellow triangle, or a yellow and blue triangle, with no blocks ever left over.”

This kept me quiet, until I worked out that I could.

How many red, blue and yellow blocks would there be?

9 Comments
Leave one →

No real challenge this week.

This runs in 200 microseconds. Here is a way of doing it without precomputing the limits on the triangular numbers:

It is slower, running in 1.7 milliseconds.

@Brian, First time I see “<" as subset check between sets, thanks.

Kind of strange break between comment lines 7 and 8!

A MiniZinc solution using Jim Randell’s minizinc.py wrapper:

Running you code with tri_nbrs as a tuple rather than a set might speed things up:

Hi John, I have already tried that and its a lot slower for me.

According to timeit this version is faster in both CPython as PyPy.

It uses [:13] iso [:14]. In some variations of the code running with [:13] was sometimes slower than running with [:14] !

It uses the fact that y is less equal to r-1 (credit Jim Randell).

Range [:18] can be be even brought down after analysis.

@Brian, as limit 1225 for y + b is not explained maybe it is better to use limit 3081 or 3160 (as y is less equal to r – 1 with max(r) = 78).

Yes, quite a bit faster, under 200 microseconds for me. The limit was one I chose arbitrarily before knowing the answer so I left it in place “because it works”. For me this teaser didn’t justify time spent on analysis.

Further analysis of the range of triangular numbers to produce single/multiple solutions can be carried out in the programme below, by changing the T_MAX value in the range to generate triangular numbers.

Let T_MAX = maximum triangular number

Single answer correct solutions were obtained for T_MAX values between 47 and 95.

The first double solution for the multiple triangular constraints was found for T_MAX = 96.Of the two solutions, the first solution was the answer to the puzzle.

Other multiple triangular configurations can be tested with higher values of T_MAX – in all cases, the first solution generated is the answer to the puzzle.