If all the 40320 (base 10) permutations of 12345678 in the hexadadecimal (base 16) representation are put in numerical order, what is the 20230th (base ten) pemutation?
Solved two ways.
First way, relying on itertools to be in order:
output: 51268473 1361478771
HEX: 51268473 = DEC:1361478771
Confirmed by running the second program which prints out:
51268473 1361478771
51268473 1361478771
-------------------
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)
from itertools import permutations
perm1to8 = []
permDEC = []
permDECsorted = []
for perm in permutations('12345678'):
perm1to8.append(''.join(perm))
print(perm1to8[20229], base2base(int(perm1to8[20229]), 16 , 10 ))
for p in perm1to8:
permDEC.append( base2base(int(p), 16 , 10 ) )
permDECsorted = sorted(permDEC)
print(base2base(int(permDEC[20229]), 10 , 16 ), permDEC[20229])
print(base2base(int(permDECsorted[20229]), 10 , 16 ), permDECsorted[20229])
|
Posted by Larry
on 2023-07-25 09:27:14 |