Given that Pi can be estimated using the function 4 * (1 – 1/3 + 1/5 – 1/7 + …) with more terms giving greater accuracy, write a function that calculates Pi to an accuracy of 5 decimal places.
(отсюда)
Моё решение
let pi precision = let rec helper pre curr digit sign = if Math.Abs(pre * 4. - curr * 4.) < precision then 4. * curr else helper curr ((if sign then (+) else (-)) curr (1. / (digit + 2.))) (digit + 2.) (not sign) helper Double.PositiveInfinity 1. 1. false pi 0.00001 |> printfn "Estimated Pi: %f"
Комментариев нет:
Отправить комментарий