A complete number is a 9-digit number that contains each of the digits 1 to 9 exactly once. The difference number of a number N is the number you get by taking the differences of consecutive digits in N and then stringing these digits together. For instance, the difference number of 25143 is equal to 3431. The complete number 124356879 has the additional property that its difference number, 12121212, consists of digits alternating between 1 and 2.
Determine all a with 3 ≤ a ≤ 9 for which there exists a complete number N with the additional property that the digits of its difference number alternate between 1 and a.
The requested valid values of "a" are just 4 and 5.
For completeness, I include Complete Numbers for a = 1, 2, 4, and 5.
In general they appear in groups of 4: each qualifying complete number has 3 mates obtained either by reversing it, or substiting each digit with its "9's complement" or both.
In the case of a=1, reversal and taking the 9's complement result in the same number, so there are only 2 of them.
a Complete number
--- ---------------
1 123456789
1 987654321
2 124356879
2 124356897
2 132457689
2 312457689
2 798653421
2 978653421
2 986754213
2 986754231
4 126598437
4 126734895
4 376215489
4 512673489
4 598437621
4 734895621
4 984376215
4 984512673
5 167238945
5 549832761
5 561278349
5 943872165
-------- code follows
from itertools import permutations
digits = list('123456789')
for alternating_a in range(1,10):
for p in permutations(digits):
diff1 = abs(int(p[0])-int(p[1]))
diff2 = abs(int(p[1])-int(p[2]))
if (diff1 == 1 and diff2 == alternating_a) or (diff1 == alternating_a and diff2 == 1) :
valid = True
for i in range(2,8,2):
if abs(int(p[i])-int(p[i+1])) != diff1:
valid = False
for i in range(3,8,2):
if abs(int(p[i])-int(p[i+1])) != diff2:
valid = False
if valid == True:
intP = int(''.join(p))
print(alternating_a,' ', intP)
|
Posted by Larry
on 2020-12-10 19:14:57 |