I already knew that the probably that two randomly chosen numbers have a common factor is 6/𝜋^2.
However, I wanted to program this to check for myself.
import math
import random
def HCF(a,b):
if (a==b) or (a*b == 0):
return b
else:
return HCF(max(a,b)%min(a,b), min(a,b))
tests = 0
successes = 0
while tests < 10000:
tests += 1
a = random.randint(1,1000000)
b = random.randint(1,1000000)
if HCF(a,b) ==1:
successes +=1
print("Experimental: ", successes/tests)
print("Theoretical: ", 6/(math.pi**2))
Experimental: 0.614
Theoretical: 0.6079271018540267