### Random Post

### Recent Posts

### Recent Comments

### Archives

### Categories

- article (11)
- enigma (1,114)
- misc (2)
- project euler (2)
- puzzle (29)
- site news (43)
- tantalizer (29)
- teaser (3)

### Site Stats

- 166,224 hits

Programming Enigma Puzzles

20 June 2012

Posted by on **From New Scientist #2870, 23rd June 2012** [link]

I was staying at my sister’s house when my niece Amy came home from school feeling special. The class had been shown how to split a whole number, T, into whole number parts in such a way that the product of the parts was the greatest, G, that could be obtained for that T. For instance, she explained, 10 could be split into ten ones, or 2 and 4 and 4, or 5 and 5, and so on, which would yield products of 1, 32, and 25 respectively. But, she warned, G exceeds 32 for T=10.

Why did she feel special? Well, each pupil in the class had been given a different number in the range 20-50 inclusive for their personal T, and she had noted that, when she added the digits of her G together, the sum was exactly half of her T, and no one else in the class had T and G with this property.

What value of T was Amy given?

[enigma1703]

Advertisements

%d bloggers like this:

This Python program uses a recursive function to constructively calculate the values of

G. But to avoid the recalculation of earlier values it caches them using the@cachedfunction decorator from theenigma.pylibrary. It runs in 35ms (without the caching it takes a lot longer).Solution:Amy’s value was T = 36.The highest product of a number > 3 must be formed from 2s and 3s that add to make the number:

For any even k > 2, k = 2m 3, k=3+2m < 3*2^m

So a bit of code (there is probably a better way to derive the optimum combination of 2s and 3s, but it's too late at night to work it out):

with formatting:

Of course: use as many 3s as possible, but leave 2 2s instead of a 3 and a 1:

And we don’t want the “set” removing duplicate digits:

Here is my version:

Solution Enigma 1703 using EXCEL:

Although it gives the right answer, there is a bug in my code above so don’t use it more generally.

I think the problem there is the

(4 // 3)expression.//is integer division, so(4 // 3)will always evaluate to1.Yes, the power of 3 needs to go before the conditional expression, not after it (which is where I had it until I did a stupid cosmetic edit while preparing to post it). I didn’t bother to post a correction as too many code fragments here can look messy.