Skip to content

Instantly share code, notes, and snippets.

@marmistrz
Created July 14, 2017 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marmistrz/787858bcc72884aff1cf881f45b8e962 to your computer and use it in GitHub Desktop.
Save marmistrz/787858bcc72884aff1cf881f45b8e962 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from math import sqrt
from sys import argv, exit
from os import getpid, system
from random import random
argc = len(argv)
if argc != 2:
print("Usage: ./pi.py num_steps")
exit(1)
inside = 0
n = int(argv[1])
for i in range(0, n):
x = random()
y = random()
if sqrt(x*x+y*y) <= 1:
inside += 1
pi = 4*inside/n
print(pi)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment