________fear_of_underscores

пятница, 7 сентября 2012 г.

Простая задачка

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"
Сможете решить короче? ;)

Комментариев нет:

Отправить комментарий