(argh, excuse me but there is not a control to create a table!!)
Number of digits vs. likelihood of repeated digits:
- None
- 9.99%
- 28%
- 50%
- 70%
- 85%
- 94%
- 98%
- 99%
- nearly 100%
The math of permutations and combinations is explained here .
The python math module functions are explained here. The ones that are relevant are the factorial, comb and perm functions. I wrote some code to re-create these. I found out that after 10 levels of recursion, Python gives you an error message. So I had to use the math module version of the factorial function.
# This defines the permutation function# and the combination functionfrom math import factorialdef perm(n,r):return factorial(n)/factorial(n-r)def comb(n,r):return factorial(n)/factorial(n-r)/factorial(r)for i in range(2,11):print(i,1-perm(10,i)/10**i)
Also, if you don't believe the math, I also wrote a piece of code that does a brute force calculation. It does take some time to work its way through all the different combinations. And it will not give the exact same results since it is not considering the numbers that start with 0.
# This program prints out how many of the numbers in a range
# have repeating digits
n = 2
flag = True
while flag:
test = 10**(n-1)
count = 0
print(test)
while test < 10**n:
a = str(test)
dupe = False
for i in range(10):
if a.count(str(i))>1:
dupe = True
if dupe:
count = count + 1
test = test +1
ratio = count / 10**n
print(n,count, ratio)
ask = input("continue? (n to end) >")
if ask == 'n':
flag = False
n = n+1So it's an interesting bit of psychology so I hope you enjoy it.
No comments:
Post a Comment