Skip to content

Instantly share code, notes, and snippets.

@peheje
Created January 2, 2023 22:32
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 peheje/395e2c9b21e898c35a9d39ba29de1816 to your computer and use it in GitHub Desktop.
Save peheje/395e2c9b21e898c35a9d39ba29de1816 to your computer and use it in GitHub Desktop.
Goat problem
let pi = System.Math.PI
let sqrt x = System.Math.Sqrt x
let inline squared x = x * x
let radiusA = 1.0
let radiusB = 1.158728 //should be "1.158728"
let area = pi*(radiusA**2.0)
let randr min max = System.Random.Shared.NextDouble() * (max - min) + min
let rand () = randr -1.0 1.0
let inside origo coordinate r =
let (ox, oy) = origo
let (x, y) = coordinate
((x-ox)**2.0) + ((y-oy)**2.0) |> sqrt < r
let findRatio length size =
let mutable insideACount = 0
let mutable insideAAndBCount = 0
for _ in 0..size-1 do
let x = rand()
let y = rand()
if inside (0.0, 0.0) (x, y) radiusA then
insideACount <- insideACount + 1
if inside (radiusA, 0.0) (x, y) length then
insideAAndBCount <- insideAAndBCount + 1
//let ratio = (insideACount |> float) / (size |> float)
//let monteArea = ratio * (radiusA**2)*4.0
(insideAAndBCount |> float) / (insideACount |> float)
let start = 0.0
let stop = 2.0
let mutable size = 1000
let mutable increment = 0.1
let mutable current = start
while true do
printfn "Trying %f with %i iterations" current size
let ratio = findRatio current size
printfn "Got ratio %f" ratio
if ratio > 0.5 then
current <- current - increment
increment <- increment * 0.95
size <- ((size |> float) * 1.1) |> int
else
current <- current + increment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment