Skip to content

Instantly share code, notes, and snippets.

@antonio-catalano
Last active April 8, 2018 00:25
Show Gist options
  • Save antonio-catalano/cfe9e4ad0ca69ee955c2439bf3eab5c3 to your computer and use it in GitHub Desktop.
Save antonio-catalano/cfe9e4ad0ca69ee955c2439bf3eab5c3 to your computer and use it in GitHub Desktop.
Another CutTheKnotMath problem
"""We want to calculate the probabilty that in a set of 3 sets, each composed of 3 different integers in the range [1,9]
we have at least one set where all the integers are odd"""
import random
print("""We want to calculate the probabilty that in a set of 3 sets, each composed of 3 different integers in the range [1,9]
we have at least one set where all the integers are odd\n\n""")
print("For example in the list [[1,4,5],[7,3,9],[2,6,8]] we have 1 sublist where all numbers are odd\n\n")
print("Let's start...")
print("Loading...")
print()
N = 1000000 # 1 millions times of iterations
count_all_odds = 0
for i in range(N):
Pr = [1,2,3,4,5,6,7,8,9]
A = []
for i in range(3): # every time we repeat the procedure, we create 3 sublists each composed of 3 integers in [1,9]
n = random.choice(Pr)
Pr.remove(n)
A.append(n)
B = []
for i in range(3):
n = random.choice(Pr)
Pr.remove(n)
B.append(n)
C = []
for i in range(3):
n = random.choice(Pr)
Pr.remove(n)
C.append(n)
List_Union = [A] + [B] + [C] # we create the list of the 3 sublists
#print(List_Union)
count = 0
for item in List_Union:
for i in item:
if i%2 == 0: # every time there is one even element in a sublist, we update count and break the for loop in item
count +=1
break
if count < 3: # in this case there is at least one sublist where all integers are odds
count_all_odds += 1 # we increase the count_all_odds of 1 unit
#print(count)
print("Probability is {} ".format(float(count_all_odds / N)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment