Teaser 2963: A result
From The Sunday Times, 7th July 2019 [link] [link]
For any number, I square the digits and then add the resulting numbers. If necessary, I keep repeating the process until I end up with a single digit, called the result. For example: 142 gives 1 + 16 + 4 = 21 which then gives 4 + 1 = 5, the result.
I have written down a two-digit number. If I tell you one of the digits [the key digit], you should be able to work out the result.
I then use a 3rd digit to get a three-digit number. The result of that number happens to be the key digit.
In increasing order, what are the three digits?
[teaser2963]
Jim Randell 5:59 pm on 5 July 2019 Permalink |
The following Python program runs in 90ms.
Run: [ @repl.it ]
from enigma import (nsplit, subsets, irange, unpack, filter_unique, printf) # process a sequence of digits def process(ds): while True: n = sum(d * d for d in ds) if n < 10: return n ds = nsplit(n) # collect the results for 2 digit numbers ss = list((ds, process(ds)) for ds in subsets(irange(0, 9), size=2, select='R') if ds != (0, 0)) # if I tell you "one of the digits is <k>" you should be able to work out the result for k in irange(0, 9): (rs, _) = filter_unique(ss, unpack(lambda ds, r: k in ds), unpack(lambda ds, r: r)) # consider possible 2-digit sequences for (ds, _) in rs: # and add an extra digit for x in irange(0, 9): # the result should be the digit <k> if process(ds + (x,)) == k: printf("k={k}: {ds} + {x}")Solution: The three digits are: 5, 6, 9.
The order of the digits in a number does not matter. 24 will have the same result as 42 (both are 2² + 4² = 20 → 2² + 0² = 4).
The key digit is 5. Any 2-digit sequence that contains a 5 has a result of 4.
The initial number is one of: 56, 59, 65, 95. An extra digit is added so the three digit sequence consists of the digits 5, 6, 9 (in some order). And this sequence has a result of 5.
Most of the 2-digit numbers give a result of 4, and if the process is allowed to continue summing the squares of the digits we see the following cycle emerge:
Numbers that eventually result in this cycle are known as “unhappy numbers” (see [ link ]).
The other 2-digit numbers give results of 1, 2, 5, 8, 9, we can also continue the process with these values:
We see that any numbers with a result of 2, 5, 8, 9 are also “unhappy”, and only those numbers that have a result of 1 are “happy”. (See OEIS A124095 [ link ]).
LikeLike
Jim Randell 3:54 pm on 7 July 2019 Permalink |
Here’s a simpler program to solve the puzzle.
Run: [ @repl.it ]
from enigma import (nsplit, seq_all_same, irange, subsets, printf) # process a sequence of digits def process(*ds): while True: n = sum(d * d for d in ds) if n < 10: return n ds = nsplit(n) # look for possible key digits for k in irange(0, 9): # must give the same result when combined with all other digits if seq_all_same(process(k, d) for d in irange(0, 9) if k + d > 0): # look for two additional digits to give a result of k for (a, b) in subsets(irange(0, 9), size=2, select='R'): if process(k, a, b) == k and k + a + b > 0: printf("k={k}: {s}", s=sorted([k, a, b]))LikeLike