Sunday Times Teaser 2559 – Inconsequential
by Victor Bryant
Published: 9 October 2011 (link)
Most whole numbers can be expressed as the sum of two or more consecutive whole numbers. For example, 35=17+18, 36=11+12+13, and 40=6+7+8+9+10.
I have written down two whole numbers. Between them they use each of the digits 0 to 9 exactly once. But neither of the numbers can be expressed as the sum of two or more consecutive whole numbers.
What are my two numbers?
One Comment
Leave one →
-
Brian Gladman permalink12345678910111213141516171819202122232425262728from itertools import combinations# If the number N is odd it can always be expressed as the sum# of two consecutive integers so N must be even. Also the only# prime that cannot be expressed as the sum of two consecutive# integers is 2. Now express N as p.m where p is a prime other# than 2. Hence in this case N = (2.k + 1).m, which is the sum# of 2.k + 1 integers m - k, m - k + 1, ... m + k - 1, m + k.# These integers are all non-negative if m > k; if m < k, then# negative terms cancel some positive terms to leave a shorter# positive sequence. So the only values that are not the sum# of consecutive integers are those that contain no odd prime# factors, namely, powers of 2.# powers of two with up to nine digitspwr2 = [str(2 ** n) for n in range(1, 30)]# powers of two without duplicate digits associated with their# digit setspwr2 = [(x, set(x)) for x in pwr2 if len(x) == len(set(x))]# consider all combinations of two powers of twofor (p1, s1), (p2, s2) in combinations(pwr2, 2):# find any pairs that use ten different digitsif not s1 & s2 and len(s1 | s2) == 10:print('Answer: {} and {}.'.format(p1, p2))