Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def distance(x,y)
Math.sqrt(x * x + y * y)
end
def in_circle(x,y)
distance(x,y) < 1
end
def random_point
rand * 2 - 1
end
def pi_from_proportion(nr_trials, nr_within_circle)
# Square's area is 4, Circle's area is PI. So PI =
(nr_within_circle / nr_trials.to_f) * 4
end
original_time = Time.now
nr_within_circle = 0
1.upto(100_000_000) do |nr|
x,y = random_point, random_point
nr_within_circle += 1 if in_circle(x,y)
if nr % 1_000_000 == 0
puts "After #{nr} trials: #{pi_from_proportion(nr, nr_within_circle)}"
end
end
puts "Calculated in #{Time.now - original_time} seconds."
After 1000000 trials: 3.141624
After 2000000 trials: 3.142106
After 3000000 trials: 3.14285866666667
After 4000000 trials: 3.142382
After 5000000 trials: 3.1420952
After 6000000 trials: 3.14186866666667
After 7000000 trials: 3.141892
After 8000000 trials: 3.141819
After 9000000 trials: 3.14213511111111
After 10000000 trials: 3.14212
After 11000000 trials: 3.14187236363636
After 12000000 trials: 3.141922
After 13000000 trials: 3.14182215384615
After 14000000 trials: 3.14183028571429
After 15000000 trials: 3.14183546666667
After 16000000 trials: 3.14169725
After 17000000 trials: 3.14165717647059
After 18000000 trials: 3.14154533333333
After 19000000 trials: 3.14161663157895
After 20000000 trials: 3.141679
After 21000000 trials: 3.14170247619048
After 22000000 trials: 3.14163272727273
After 23000000 trials: 3.14169147826087
After 24000000 trials: 3.14167316666667
After 25000000 trials: 3.14159104
After 26000000 trials: 3.14161338461538
After 27000000 trials: 3.14170711111111
After 28000000 trials: 3.14168214285714
After 29000000 trials: 3.14162910344828
After 30000000 trials: 3.14166266666667
After 31000000 trials: 3.14158387096774
After 32000000 trials: 3.1415385
After 33000000 trials: 3.14149709090909
After 34000000 trials: 3.14151129411765
After 35000000 trials: 3.1413952
After 36000000 trials: 3.14147355555556
After 37000000 trials: 3.14149437837838
After 38000000 trials: 3.14149884210526
After 39000000 trials: 3.14141507692308
After 40000000 trials: 3.1414097
After 41000000 trials: 3.14142068292683
After 42000000 trials: 3.14144819047619
After 43000000 trials: 3.14147534883721
After 44000000 trials: 3.14151445454545
After 45000000 trials: 3.14153493333333
After 46000000 trials: 3.14150556521739
After 47000000 trials: 3.14150825531915
After 48000000 trials: 3.14154733333333
After 49000000 trials: 3.14159983673469
After 50000000 trials: 3.1416192
After 51000000 trials: 3.14163552941176
After 52000000 trials: 3.14161584615385
After 53000000 trials: 3.14166143396226
After 54000000 trials: 3.14171325925926
After 55000000 trials: 3.14170450909091
After 56000000 trials: 3.14167935714286
After 57000000 trials: 3.14166933333333
After 58000000 trials: 3.14165475862069
After 59000000 trials: 3.14165369491525
After 60000000 trials: 3.14166213333333
After 61000000 trials: 3.14167829508197
After 62000000 trials: 3.14165348387097
After 63000000 trials: 3.14165657142857
After 64000000 trials: 3.141637875
After 65000000 trials: 3.14164301538462
After 66000000 trials: 3.14166581818182
After 67000000 trials: 3.14168841791045
After 68000000 trials: 3.14171117647059
After 69000000 trials: 3.14173049275362
After 70000000 trials: 3.14177982857143
After 71000000 trials: 3.14179177464789
After 72000000 trials: 3.14176411111111
After 73000000 trials: 3.14177452054795
After 74000000 trials: 3.14174837837838
After 75000000 trials: 3.14173744
After 76000000 trials: 3.14170705263158
After 77000000 trials: 3.14170077922078
After 78000000 trials: 3.14169158974359
After 79000000 trials: 3.14169584810127
After 80000000 trials: 3.14172585
After 81000000 trials: 3.14172637037037
After 82000000 trials: 3.14177853658537
After 83000000 trials: 3.14172563855422
After 84000000 trials: 3.14174547619048
After 85000000 trials: 3.14176047058824
After 86000000 trials: 3.14175460465116
After 87000000 trials: 3.1417484137931
After 88000000 trials: 3.14171127272727
After 89000000 trials: 3.14173339325843
After 90000000 trials: 3.14173666666667
After 91000000 trials: 3.14172975824176
After 92000000 trials: 3.14172660869565
After 93000000 trials: 3.14173096774194
After 94000000 trials: 3.14173744680851
After 95000000 trials: 3.14174214736842
After 96000000 trials: 3.14174591666667
After 97000000 trials: 3.14173051546392
After 98000000 trials: 3.14174040816327
After 99000000 trials: 3.14174254545455
After 100000000 trials: 3.1417136
Calculated in 261.702801 seconds.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment