3 people like it.

Calculate PI number

PI number calculation based on the wikipedia page(http://en.wikipedia.org/wiki/Pi#cite_note-59). I used Newton's , Machine's and Ramanujan's formula. (updated: line 21: Seq.take => Seq.truncate)

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
60: 
61: 
62: 
63: 
64: 
65: 
66: 
67: 
68: 
69: 
70: 
71: 
72: 
73: 
74: 
75: 
76: 
77: 
78: 
79: 
80: 
81: 
82: 
83: 
84: 
85: 
86: 
87: 
#r "FSharp.PowerPack.dll"

open System
open Microsoft.FSharp.Math

/// convert int to bignum
let toBigNum = bignum.FromInt

/// get integer part of a bignum
let floorBigNum = bignum.ToBigInt>>bignum.FromBigInt

/// convert bignum to string with the specified fraction length
let toString fractionLength (value:bignum) =
  let integer = floorBigNum value
  seq { 
  let fraction = ref ((value - integer) * 10N)
  while !fraction <> 0N do
    let digit = floorBigNum !fraction
    fraction := (!fraction - digit) * 10N
    yield string digit }
  |> Seq.truncate fractionLength
  |> String.concat ""
  |> fun fraction -> string integer + "." + fraction

/// print bignum
let print len = printfn"%s"<<toString len

/// Isaac Newton 1665–66
let newton n = 
  seq { 
  let a = ref 1N
  let b = ref 2N
  for i in 1 .. n do
    let i = toBigNum i * 2N
    a := !a * (i-1N)
    b := !b * 4N * i
    yield !a / !b / (i+1N)
  }
  |> Seq.sum
  |> (fun v -> v * 6N + 3N)

newton 200 |> print 100
// 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

/// calculate arctan value
let arctan n (bn:bignum) =
  seq {
  yield bn
  let a = ref bn
  for i in 1 .. n do
    a := - !a * bn * bn 
    yield !a / (2N * toBigNum i + 1N)
  }
  |> Seq.sum

/// John Machin 1706
let matchin n = 16N * arctan(3*n) (1N/5N) - 4N * arctan n (1N/239N)


matchin 24 |> print 100
// 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

/// Ramanujan
let ramanujan n = 
  seq {
    yield 1123N
    let a = ref 1N
    for i in 1 .. n do
      let i = toBigNum i
      a := 
        - !a 
        * List.reduce ( * ) [4N*i-3N..4N*i]
        / BigNum.PowN(i,4) / 199148544N
      yield !a * (21460N * i + 1123N)
  }
  |> Seq.sum
  |> fun x -> 3528N / x

ramanujan 17 |> print 100
// 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679

let pi1 = newton 200 |> toString 100
let pi2 = matchin 24 |> toString 100
let pi3 = ramanujan 17 |> toString 100

(pi1 = pi2 && pi2 = pi3) |> printfn "%b"
// true
namespace System
namespace Microsoft
namespace Microsoft.FSharp
type Math =
  static val PI : float
  static val E : float
  static member Abs : value:sbyte -> sbyte + 6 overloads
  static member Acos : d:float -> float
  static member Asin : d:float -> float
  static member Atan : d:float -> float
  static member Atan2 : y:float * x:float -> float
  static member BigMul : a:int * b:int -> int64
  static member Ceiling : d:decimal -> decimal + 1 overload
  static member Cos : d:float -> float
  ...

Full name: System.Math
val toBigNum : (int -> obj)

Full name: Script.toBigNum


 convert int to bignum
val floorBigNum : (obj -> obj)

Full name: Script.floorBigNum


 get integer part of a bignum
val toString : fractionLength:int -> value:obj -> string

Full name: Script.toString


 convert bignum to string with the specified fraction length
val fractionLength : int
val value : obj
val integer : obj
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

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

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

Full name: Microsoft.FSharp.Collections.seq<_>
val fraction : obj ref
Multiple items
val ref : value:'T -> 'T ref

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

--------------------
type 'T ref = Ref<'T>

Full name: Microsoft.FSharp.Core.ref<_>
val digit : obj
Multiple items
val string : value:'T -> string

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

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
module Seq

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

Full name: Microsoft.FSharp.Collections.Seq.truncate
Multiple items
type String =
  new : value:char -> string + 7 overloads
  member Chars : int -> char
  member Clone : unit -> obj
  member CompareTo : value:obj -> int + 1 overload
  member Contains : value:string -> bool
  member CopyTo : sourceIndex:int * destination:char[] * destinationIndex:int * count:int -> unit
  member EndsWith : value:string -> bool + 2 overloads
  member Equals : obj:obj -> bool + 2 overloads
  member GetEnumerator : unit -> CharEnumerator
  member GetHashCode : unit -> int
  ...

Full name: System.String

--------------------
String(value: nativeptr<char>) : unit
String(value: nativeptr<sbyte>) : unit
String(value: char []) : unit
String(c: char, count: int) : unit
String(value: nativeptr<char>, startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int) : unit
String(value: char [], startIndex: int, length: int) : unit
String(value: nativeptr<sbyte>, startIndex: int, length: int, enc: Text.Encoding) : unit
val concat : sep:string -> strings:seq<string> -> string

Full name: Microsoft.FSharp.Core.String.concat
val fraction : string
val print : len:int -> (obj -> unit)

Full name: Script.print


 print bignum
val len : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val newton : n:int -> obj

Full name: Script.newton


 Isaac Newton 1665–66
val n : int
val a : obj ref
val b : obj ref
val i : int
val i : obj
val sum : source:seq<'T> -> 'T (requires member ( + ) and member get_Zero)

Full name: Microsoft.FSharp.Collections.Seq.sum
val v : obj
val arctan : n:int -> bn:int -> int

Full name: Script.arctan


 calculate arctan value
val bn : int
val a : int ref
val matchin : n:int -> obj

Full name: Script.matchin


 John Machin 1706
val ramanujan : n:int -> obj

Full name: Script.ramanujan


 Ramanujan
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val reduce : reduction:('T -> 'T -> 'T) -> list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.reduce
val x : obj
val pi1 : string

Full name: Script.pi1
val pi2 : string

Full name: Script.pi2
val pi3 : string

Full name: Script.pi3

More information

Link:http://fssnip.net/8M
Posted:5 years ago
Author:nagat01
Tags: mathematics