0 people like it.

Pi - Using Nilakantha's accelerated series

Calculate the value of Pi, using the Nilakantha series

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
let nPi =     
    let piSeq = seq{
        yield 3.0
        let rec pi start sign = seq{
            yield (sign * 4.0) / ( start * (start + 1.0) * (start + 2.0))
            yield! pi (start + 2.0) (sign * -1.0)
        }

        yield! pi 2.0 1.0
    }
    piSeq |> Seq.take 10 |> Seq.sum
val nPi : float

Full name: Script.nPi
val piSeq : seq<float>
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val pi : (float -> float -> seq<float>)
val start : float
val sign : float
module Seq

from Microsoft.FSharp.Collections
val take : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.take
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/7X6
Posted:5 years ago
Author:Apurva
Tags: math