N is a tridecimal (base 13) positive integer using the digits 1 to 5 at least once, but at most twice. All the digits of N are nonzero and none of the digits from 6 to C occur in N.
Determine the probability that N is divisible by the tridecimal number 22.
The probability that N is divisible by the tridecimal number 22 is 0.020238584944467296
First, programmatically create a list of strings containing either 1 or 2 of each of the 5 digits. There are 32 of them.
The 32 strings with either 1 or 2 of each digit:
['12345', '123451', '123452', '1234512', '123453', '1234513', '1234523', '12345123', '123454', '1234514', '1234524', '12345124', '1234534', '12345134', '12345234', '123451234', '123455', '1234515', '1234525', '12345125', '1234535', '12345135', '12345235', '123451235', '1234545', '12345145', '12345245', '123451245', '12345345', '123451345', '123452345', '1234512345']
Next, for each of these, create every permutation possible. But since Python's itertools permutations would make many duplicates, use a function to remove the duplicates. This is done by the function uniqueGen() which is a Generator.
Then for each number increment denominator, and if divisible by 28 also increment numerator.
The program finds 291720 numbers that fit the constraints (denominator).
Of these, 5904 were divisible by 28 (22 in base 13).
The probability is thus 5904/291720 = 0.020238584944467296
Compare that to 1/28 which is 0.0357142857142857...
---- functions ----
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)
def uniqueGen(iterable):
uniqueSet = set()
for i in iterable:
if i in uniqueSet:
continue
uniqueSet.add(i)
yield i
---- program code ----
one2fiveList = []
one2five = '12345'
for i in range(32):
extra = ''
for j in range(5):
if (i >> j & 1) != 0:
extra = extra + str(j+1)
one2fiveList.append(one2five + extra)
print(one2fiveList)
numerator = 0
denominator = 0
from itertools import permutations
for pattern in one2fiveList:
for scramble in uniqueGen(permutations(pattern)):
num = base2base( int(''.join(scramble)), 13,10)
denominator += 1
if num%28 == 0:
numerator += 1
print(numerator, denominator, numerator/denominator )
|
Posted by Larry
on 2021-03-15 10:50:54 |