## Teaser 3131: Heads up savings

**From The Sunday Times, 25th September 2022** [link] [link]

Little Spencer saves 5p coins in a jar, and when they reach £10, deposits them in his savings account. He likes playing with the coins. In one game, after first turning them all heads up, he places them in a row on the table. Starting from the left, he then turns over every 2nd coin until he has reached the end of the row. He then again starts from the left, and this time turns over every 3rd coin. He repeats this for every 4th, 5th coin etc, until finally he turned over just one coin, the last in the row.

At the end of the game I could see that if Spencer had exactly 75 per cent more coins he would have an increase of 40 per cent in the number showing heads. However, if he had exactly 50 per cent fewer coins, he would have a decrease of 40 per cent in the number showing heads.

What is the value of his 5p savings?

There are now 750 **Teaser** puzzles available on the **S2T2** site.

[teaser3131]

## Jim Randell 5:09 pm

on23 September 2022 Permalink |(See also:

Puzzle #08).Here is a constructive solution.

It runs in 52ms. (Internal runtime is 409µs).

Run:[ @replit ]Solution:The total value of the coins is £1.40.Spencer has 28 coins.

After performing the procedure there are 5 coins remaining heads up (= coins 1, 4, 9, 16, 25).

If he had 1.75× the number of coins (= 49 coins), then 7 would remain heads up (= coins 1, 4, 9, 16, 25, 36, 49).

And if he had 0.5× the number of coins (= 14 coins), then 3 would remain heads up (= coins 1, 4, 9).

LikeLike

## Jim Randell 5:46 pm

on23 September 2022 Permalink |Using the fact that the coins that remain heads up are exactly those in the perfect square numbered positions (numbering from 1), we can get a shorter (and faster) program.

This Python program runs in 51ms. (Internal runtime is 221µs).

Run:[ @replit ]LikeLike

## NigelR 10:50 am

on26 September 2022 Permalink |Irrespective of the number of times coin n in the row is flipped, its final H/T depends on whether n has an odd or even number of factors. (I hadn’t spotted the elegance of the perfect squares!).

PS: I think the answer sought was actually the value of the coins, not the number.

LikeLike

## Jim Randell 2:24 pm

on26 September 2022 Permalink |@NigelR: I left determining the total value of a certain number of 5p coins as a simple exercise for the reader ;-).

You could shorten this line of code somewhat:

`(True if ... else False)`

is just the same as evaluating`...`

(in a boolean context):and then in the list comprehension,

`(1 if ... else 0)`

is also the same as`...`

(in a boolean context; in Python`False`

and`True`

are just`0`

and`1`

in disguise):and we don’t need to construct the list, just to count the number of 1’s in it:

Also note that Python’s builtin

`range`

function doesnotinclude the endpoint. So if you want to go to 350 in line 4 you need to specify a`stop`

value of 351. Similarly in line 8, to check up to 200 coins you need to specify a`stop`

value of 201.(I tend to use the

inclusive`irange()`

function from theenigma.pylibrary, which includes both endpoints, to avoid this kind of “fencepost” error).LikeLike

## NigelR 8:40 pm

on26 September 2022 Permalink |JIm: Thank you so much for taking the time to unpick my messy code and for your very helpful advice – your countfac is much simpler and I’m now wiser! I couldn’t work out what on earth I’d done in the lambda an hour after writing it!

Agree on the stop value of 351, but I did think about the 200/201 and concluded that Spencer would have banked the lot if it had reached 200, and hence he would only have been playing with up to 199 coins. Perhaps I’m overthinking it!

LikeLike

## Jim Randell 10:08 am

on27 September 2022 Permalink |Yes the (200, 350) case is a bit of a grey area. I decided he might like one last play with his coins before he banked them, so I might as well check it, as I prefer solutions that exhaustively explore the solution space.

As it turns out it doesn’t provide an answer, so it doesn’t really matter if you check it or not.

LikeLike

## NigelR 10:58 am

on26 September 2022 Permalink |… and,of course, the 75% greater number is only hypothetical and hence can be greater than 200. My line 4 should go to 350, and line 10 is unnecessary.

LikeLike