Commit 914c407

bryfry <bryon@fryer.io>
2017-03-13 21:25:45
init
chart.png
Binary file
dice.csv
@@ -0,0 +1,62 @@
+n trials, max rand, variance, min, q1, q2, q3, max
+500, 4, 0.009082, 3.345748, 3.643396, 3.707765, 3.770678, 4.093871
+500, 8, 0.004638, 3.100868, 3.267439, 3.312884, 3.358302, 3.619453
+500, 16, 0.004140, 3.081180, 3.233104, 3.277715, 3.321056, 3.565370
+500, 32, 0.003282, 2.944575, 3.120939, 3.162332, 3.199829, 3.377402
+500, 64, 0.003490, 2.979218, 3.126018, 3.165748, 3.205304, 3.409972
+500, 128, 0.003215, 2.957424, 3.110855, 3.150743, 3.188964, 3.390318
+500, 256, 0.003175, 2.970443, 3.110855, 3.151527, 3.188964, 3.450328
+500, 512, 0.003151, 2.940329, 3.105850, 3.143477, 3.178209, 3.358302
+500, 1024, 0.003238, 2.953122, 3.105850, 3.145609, 3.183573, 3.377402
+500, 2048, 0.003176, 2.966084, 3.105850, 3.143968, 3.183573, 3.370999
+500, 4096, 0.003445, 2.966084, 3.105850, 3.145536, 3.183573, 3.377402
+500, 8192, 0.003183, 2.979218, 3.105850, 3.144454, 3.183573, 3.358302
+500, 16384, 0.003087, 2.936101, 3.105850, 3.143590, 3.178209, 3.429972
+500, 32768, 0.003223, 2.983635, 3.100868, 3.143304, 3.178209, 3.352008
+500, 65536, 0.003209, 2.927700, 3.105850, 3.142804, 3.178209, 3.352008
+500, 131072, 0.003169, 2.940329, 3.105850, 3.143472, 3.178209, 3.352008
+500, 262144, 0.003284, 2.974821, 3.105850, 3.143833, 3.183573, 3.333333
+500, 524288, 0.003197, 2.961744, 3.105850, 3.142136, 3.178209, 3.364633
+500, 1048576, 0.003204, 2.948839, 3.105850, 3.142607, 3.178209, 3.377402
+500, 2097152, 0.003184, 2.944575, 3.100868, 3.140655, 3.178209, 3.364633
+500, 4194304, 0.003122, 2.970443, 3.105850, 3.143432, 3.178209, 3.377402
+500, 8388608, 0.003196, 2.944575, 3.105850, 3.143445, 3.183573, 3.364633
+500, 16777216, 0.003128, 2.927700, 3.105850, 3.142946, 3.178209, 3.339523
+500, 33554432, 0.003147, 2.948839, 3.100868, 3.141264, 3.178209, 3.383841
+500, 67108864, 0.003379, 2.974821, 3.100868, 3.143197, 3.178209, 3.370999
+500, 134217728, 0.003164, 2.957424, 3.105850, 3.144426, 3.183573, 3.370999
+500, 268435456, 0.003347, 2.957424, 3.105850, 3.143901, 3.183573, 3.383841
+500, 536870912, 0.003276, 2.944575, 3.105850, 3.144547, 3.183573, 3.370999
+500, 1073741824, 0.003219, 2.961744, 3.105850, 3.143703, 3.183573, 3.364633
+500, 2147483648, 0.003153, 2.970443, 3.105850, 3.143444, 3.183573, 3.383841
+500, 4294967296, 0.003280, 2.961744, 3.105850, 3.142912, 3.178209, 3.390318
+500, 8589934592, 0.003123, 2.974821, 3.100868, 3.141991, 3.178209, 3.390318
+500, 17179869184, 0.003121, 2.961744, 3.105850, 3.142819, 3.178209, 3.377402
+500, 34359738368, 0.003241, 2.961744, 3.105850, 3.143633, 3.183573, 3.358302
+500, 68719476736, 0.003297, 2.979218, 3.105850, 3.143735, 3.183573, 3.352008
+500, 137438953472, 0.003099, 2.970443, 3.105850, 3.142690, 3.178209, 3.364633
+500, 274877906944, 0.003167, 2.936101, 3.105850, 3.143366, 3.178209, 3.345748
+500, 549755813888, 0.003170, 2.966084, 3.105850, 3.142536, 3.178209, 3.364633
+500, 1099511627776, 0.003165, 2.953122, 3.105850, 3.142345, 3.178209, 3.370999
+500, 2199023255552, 0.003291, 2.966084, 3.105850, 3.143044, 3.178209, 3.377402
+500, 4398046511104, 0.003259, 2.927700, 3.105850, 3.144137, 3.183573, 3.345748
+500, 8796093022208, 0.003266, 2.948839, 3.105850, 3.142895, 3.178209, 3.370999
+500, 17592186044416, 0.003290, 2.944575, 3.105850, 3.143724, 3.178209, 3.364633
+500, 35184372088832, 0.003224, 2.970443, 3.105850, 3.144288, 3.183573, 3.457194
+500, 70368744177664, 0.003210, 2.961744, 3.105850, 3.143345, 3.178209, 3.364633
+500, 140737488355328, 0.003086, 2.961744, 3.105850, 3.143727, 3.178209, 3.416599
+500, 281474976710656, 0.003177, 2.966084, 3.105850, 3.143606, 3.178209, 3.364633
+500, 562949953421312, 0.003194, 2.970443, 3.105850, 3.142957, 3.178209, 3.364633
+500, 1125899906842624, 0.003225, 2.953122, 3.100868, 3.142054, 3.178209, 3.358302
+500, 2251799813685248, 0.003323, 2.940329, 3.105850, 3.144914, 3.183573, 3.390318
+500, 4503599627370496, 0.003144, 2.927700, 3.105850, 3.142812, 3.178209, 3.396831
+500, 9007199254740992, 0.003283, 2.936101, 3.105850, 3.143568, 3.178209, 3.396831
+500, 18014398509481984, 0.003193, 2.944575, 3.105850, 3.143390, 3.183573, 3.370999
+500, 36028797018963968, 0.003205, 2.948839, 3.100868, 3.141424, 3.178209, 3.352008
+500, 72057594037927936, 0.003233, 2.944575, 3.105850, 3.142977, 3.178209, 3.345748
+500, 144115188075855872, 0.003123, 2.983635, 3.105850, 3.143944, 3.178209, 3.333333
+500, 288230376151711744, 0.003125, 2.970443, 3.105850, 3.143383, 3.178209, 3.339523
+500, 576460752303423488, 0.003124, 2.961744, 3.105850, 3.143612, 3.178209, 3.383841
+500, 1152921504606846976, 0.003169, 2.953122, 3.105850, 3.142872, 3.178209, 3.377402
+500, 2305843009213693952, 0.003241, 2.966084, 3.105850, 3.142651, 3.178209, 3.358302
+500, 4611686018427387904, 0.003095, 2.953122, 3.105850, 3.143400, 3.178209, 3.390318
main.go
@@ -0,0 +1,57 @@
+package main
+
+import (
+	"fmt"
+	"github.com/montanaflynn/stats"
+	"math"
+	"math/big"
+	"math/rand"
+	"time"
+)
+
+type Trial struct {
+	A       *big.Int
+	B       *big.Int
+	Coprime bool
+}
+
+func main() {
+	var num_trials = 500
+	var i uint64
+
+	fmt.Printf("n trials, max rand, variance, min, q1, q2, q3, max\n")
+	for i = 1; i < 100; i++ {
+		var max_rand = big.NewInt(2 << i)
+		var exp_pis []float64
+		for i := 1; i <= 4096; i++ {
+			exp_pis = append(exp_pis, Experiment(max_rand, num_trials))
+		}
+		max, _ := stats.Max(exp_pis)
+		min, _ := stats.Min(exp_pis)
+		mean, _ := stats.Mean(exp_pis)
+		variance, _ := stats.Variance(exp_pis)
+		q, _ := stats.Quartile(exp_pis)
+		fmt.Printf("%d, %d, %f, %f, %f, %f, %f, %f\n", num_trials, max_rand, variance, min, q.Q1, mean, q.Q3, max)
+	}
+}
+
+func Experiment(max_rand *big.Int, num_trials int) (exp_pi float64) {
+	trials := make([]Trial, num_trials)
+	r := rand.New(rand.NewSource(time.Now().UnixNano()))
+	cc := 0 // coprime count
+	for i, t := range trials {
+		var C *big.Int
+		t.A, t.B, C = big.NewInt(0), big.NewInt(0), big.NewInt(0)
+		trials[i].A = t.A.Rand(r, max_rand)
+		trials[i].B = t.B.Rand(r, max_rand)
+		C = C.GCD(nil, nil, t.A, t.B)
+		if C.Cmp(big.NewInt(1)) == 0 {
+			trials[i].Coprime = true
+			cc += 1
+		}
+	}
+	//for _, t := range trials {
+	//	fmt.Printf("%s, %s, %s, %t\n", t.A.String(), t.B.String(), t.C.String(), t.Coprime)
+	//}
+	return math.Sqrt(6 / (float64(cc) / float64(num_trials)))
+}
README.md
@@ -0,0 +1,13 @@
+# Pi via Rand
+
+Inspired by standupmaths video https://www.youtube.com/watch?v=RZBhSi_PwHU
+
+My question: If you could increase the sides of the dice (keeping dice rolls constant), would it actually improve the calculation of Pi?
+
+Answer: Not really, 120 sided dice was probably good enough, you just need more rolls of the dice.
+
+![Pi via 500 dice throws](chart.png)
+
+Google spreadsheet with a nice chart: https://goo.gl/Mkoqf6
+
+