Daily Shaarli

All links of one day in a single page.

July 7, 2020

Approximations of π - Wikipedia
thumbnail

In Go

package main

import (
    "fmt"
    "math"
    "math/rand"
    "time"
)

func main() {
    n := 10_000_000
    r := rand.New(rand.NewSource(time.Now().UnixNano()))
    count := 0

    for i := 0; i < n; i++ {
        x := r.Float64()
        y := r.Float64()

        pytha := math.Sqrt(x*x + y*y)

        if pytha <= 1 {
            count++
        }
    }

    fmt.Println("Pi = ", float64(count)/float64(n)*4)
}

It's beautiful