Determine the value of the respective smallest and largest value of a six digit octadecimal (base 18) positive integer N such that when N is multiplied by an octadecimal digit, the result is a 7-digit octadecimal positive integer having precisely six identical digits.
I got slightly different answers than Charlie did.
base-18 decimal product
N mult N mult base-18 decimal
123457 * H 2118409 * 17 111111B 36012953
HGFEDC * H 33894534 * 17 GGGGGG6 576207078
-------------------
def base2base(n,a,b):
""" input n which is a string of the number to be changed
'a' is an integer representing the current base
to be changed to base b
"""
def dec2base(i,base):
""" INPUT integer in base 10, return string
of Base base equivalent. """
convertString = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'
if i < base:
return convertString[i]
else:
return dec2base(i//base,base) + convertString[i%base]
if a == 10:
return dec2base(int(n),b)
if b == 10:
return int(str(n),a)
elif b != 10:
return base2base(int(str(n),a),10,b)
first6 = 18**5
last6 = 18**6 - 1
first7 = 18**6
last7 = 18**7 - 1
smallest = []
largest = []
smallFound = False
largeFound = False
for x in range(first6, last6 + 1):
if smallFound:
break
for m in range(2,18):
p = x*m
if p < first7:
continue
if p > last7:
break
p18 = base2base(p,10,18)
if len(set(p18)) != 2:
continue
a = sorted(list(set(p18)))[0]
b = sorted(list(set(p18)))[1]
if abs( p18.count(a) - p18.count(b) ) != 5:
continue
print(p18, '=', base2base(m,10,18),'*', base2base(x,10,18))
smallest.append([p, p18, m, base2base(m,10,18), x, base2base(x,10,18)])
smallFound = True
break
for x in range(last6, first6 - 1, -1): # Reverse order
if largeFound:
break
for m in range(2,18):
p = x*m
if p < first7:
continue
if p > last7:
break
p18 = base2base(p,10,18)
if len(set(p18)) != 2:
continue
a = sorted(list(set(p18)))[0]
b = sorted(list(set(p18)))[1]
if abs( p18.count(a) - p18.count(b) ) != 5:
continue
print(p18, '=', base2base(m,10,18),'*', base2base(x,10,18))
largest.append([p, p18, m, base2base(m,10,18), x, base2base(x,10,18)])
largeFound = True
break
print(smallest)
print(largest)
111111B = H * 123457
GGGGGG6 = H * HGFEDC
[[36012953, '111111B', 17, 'H', 2118409, '123457']]
[[576207078, 'GGGGGG6', 17, 'H', 33894534, 'HGFEDC']]
|
Posted by Larry
on 2023-07-22 11:47:54 |