# Sunday Times Teaser 3226 – Prime Choice

*by Andrew Skidmore*

#### Published Thursday July 18 2024 (link)

I have written down three numbers, which together use each of the digits from 0 to 9 once.

All the numbers are greater than one hundred and the two smaller numbers are prime. I have also written down the product of the three numbers. If I told you how many digits the product contains you wouldnâ€™t be able to tell me the value of the product. However, if I also told you whether the product is odd or even then you would be able to work out all the three numbers.

What, in increasing order, are my three numbers?

6 Comments
Leave one →

Brian, why not use the setdefault() method in place of the code in lines 19 – 22?

Hi John,

It is a speed issue. The code above runs in 36 milliseconds on my laptop.

If I use the code:

the run time is then 1.3 seconds!

The reason is that, when the setdefault version is used, the dictionary

on the left is a NEW dictionary every time and this takes time to set

up. In my version a new directory is only set up once, after which an

existing directory is simply modified.

This teaser turns out to be an extreme example of this sort of effect.

Your use of defauldict here avoids this problem completely (I was just

avoiding an import but since I needed one anyway this was pointless).

I was in the habit of using get() for dictionaries in order to avoid an import

of defaultdict. Now I know this choice is not without consequences so I will

start using defaultdict again. A good find by Brian.

In this Teaser, the dictionary keys are totally and easily predictable because the product of the three numbers can only have 8, 9 or 10 digits and be odd or even. Replacing line 12 above with

solves the ‘defaultdict’, ‘setdefault’, ‘get’ conundrum, makes import of defaultdict unnecessary and results in a speed improvement.