Teaser 2531: [Harshad numbers]
From The Sunday Times, 27th March 2011 [link] [link]
A Harshad number (or H-number) is any number that is divisible by the sum of its digits. Using each non-zero digit just the once, I have written down a 9-figure H-number. Reading from left to right, it also consists of three 2-figure H-numbers followed by a 3-figure H-number. Again working from left to right through the 9-figure number, the last five digits form a 5-figure H-number. Reversing the order of the first five digits of the 9-figure number also gives a 5-figure H-number.
What is the 9-figure number?
This puzzle was originally published with no title.
[teaser2531]
Jim Randell 12:34 pm on 17 July 2022 Permalink |
See: [@wikipedia].
We can use the [[
SubstitutedExpression]] solver from the enigma.py library to solve this puzzle.The following run file executes in 72ms. The internal runtime of the generated program is 3.1ms).
Run: [ @replit ]
#! python3 -m enigma -rr SubstitutedExpression --digits="1-9" # is <n> an H-number? --code="H = lambda n: n % dsum(n) == 0" # the 9-digit number is: abcdefghi "H({abcdefghi})" # three 2-digit H-numbers and a 3-digit H-number "H({ab})" "H({cd})" "H({ef})" "H({ghi})" # 5-digit H-numbers "H({efghi})" "H({edcba})" --answer="{abcdefghi}"Solution: The 9-digit number is: 273684915.
LikeLike
GeoffR 2:16 pm on 17 July 2022 Permalink |
LikeLike
Frits 3:40 pm on 17 July 2022 Permalink |
from itertools import permutations from functools import reduce # convert digits sequence to number d2n = lambda s: reduce(lambda x, y: 10 * x + y, s) # calculate H-number H = lambda s: d2n(s) % sum(s) == 0 i = 5 # as abcdefghi is divisible by 45 # ghi: sum(g,h) must be even as sum(g,h,i) must be odd # efghi: sum(e,f) must be even as sum(e,f,g,h,i) must be odd # ef: e and f not both odd otherwise 10*d + e is not divisible by e + f # so e and f are both even # ab: a and b not both odd # cd: c and d not both odd # g an h must be both odd otherwise a, b, c and d must all be odd # ....eeoo5 # edcba : a must be even as sum(e,d,c,b,a) is even # e...eeoo5 # as c and d are not both odd b must be odd # eo..eeoo5 for (b, g, h) in permutations([1, 3, 7, 9], 3): if not H((g, h, i)): continue for (a, e, f) in permutations([2, 4, 6, 8], 3): if not H((a, b)): continue if not H((e, f)): continue if not H((e, f, g, h, i)): continue rest = set(range(1, 10)).difference({a, b, e, f, g, h, i}) for (c, d) in permutations(rest): if not H((c, d)): continue if not H((e, d, c, b, a)): continue if not H((a, b, c, d, e, f, g, h, i)): continue print("the 9-figure number:", d2n((a, b, c, d, e, f, g, h, i)))LikeLike
GeoffR 6:43 pm on 17 July 2022 Permalink |
I carried out some further analysis to see how the number of Harshad numbers reduced with different arrangements of numbers.
1) For numbers ab, cd, ef, ghi and abcdefghi, this gave 96 possible 9-digit Harshad numbers.
2) Adding abcde to (1) above, I found 16 possible Harshad numbers for abcdefghi:
3) Finally, adding number edcba to (2) above, this gave the single 9-digit answer of 273684915.
(top row, middle value)
Interesting to note how the 16 solutions illustrate aspects of analysis in Frits code.
LikeLike