Calculating pi to learn Julia

I love standup maths, the man is too good, I will be very soon buying and reading his books. His yearly tradition of calculating value of pi in absurd ways every year on pi day is something I enjoy a lot.

Similar to what we did for multiplicative persistence, today I will be writing code for what my man does by hand.

The concept we will be using to figure out pi today is the fact that the probability of two random numbers being coprime is:

$$P(a,b) = \frac{6}{\pi^2}$$

Anyway we have ability to generate random numbers using computers, so we can harness that and do this fun activity to calculate the value of pi in this fun way.

As a rule any coding problem I encounter, I try to code it in python as that is what I am most comfortable with, then I move to the language in which I actually need to write the code in.

I am using euclidean method to find the GCD and also printing the elapsed time to see how long it takes for the program to run. Intentionally chose large numbers for randrange, as bigger the number, higher the accuracy, you can take smaller numbers and see how that changes the dynamic, also the iterations also matter too in determining the accuracy of results.

import random
import math
import time

start = time.time() 
count = 0
total = 10

def gcd(a,b):
    while(b):
        a,b = b, a%b
    return abs(a)

for i in range(0,total):   
    left = random.randint(0,10000000000)
    right = random.randint(0,10000000000)
    if(gcd(left, right)==1):
        count+=1
end = time.time() 
print(f"total pairs with gcd as 1: {count}")
print(f"total numbers: {total}")
print(f"time taken to run program is {end-start}")
print(math.sqrt(6/(count/total)))

Now onto writing the same code in Julia, I should be embarrassed a little for being so confused by the amount of times I had issues due to end statements, but still I did succeed in writing the code snippet.

import Base.time

count = 0
total = 10000000
function gcd(a, b)
    while(b!=0)
        a,b = b,a%b
    end
    return a
end

println(gcd(60,48))
start_time = time()
for i in 1:total
    left = rand(1:total)
    right = rand(1:total)
    if gcd(left, right) == 1
        global count += 1
    end
end
elapsed_time = time() - start_time
println("total pairs with gcd as 1: ",count)
println("total numbers ",total)
println("Time taken to find result: ", elapsed_time, " seconds")
println(sqrt(6/(count/total)))

It can be observed here too Julia seems to be faster than python, in a specific case with 10000000 as an example, was about 7 times faster.

Julia has been a language that has fascinated me a lot and I am finally doing something about that fascination by trying out stuff like this, one of my plans with continuing this exercise is to have a julia package to my name. If that package even has a slight impact on how it improves julia devs lives, I would be super happy.

0
Subscribe to my newsletter

Read articles from Sammith S Bharadwaj directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Sammith S Bharadwaj
Sammith S Bharadwaj