0 people like it.

Factorial using Int64, Double and BigInteger

Recursive Factorial using Int64, Double and BigInteger with execution time.

 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: 
open System  
open System.Diagnostics  
open System.Numerics  
  
// Long Factorial  
let rec FactorialInt64(n:int): int64 =  
    match n with  
    | 1 -> int64(1)  
    | n -> int64(n) * FactorialInt64(n - 1)  
  
// Double Factorial  
let rec FactorialDouble(n:int): double =  
    match n with  
    | 1 -> double(1)  
    | n -> double(n) * FactorialDouble(n - 1)  
  
// BigInteger Factorial  
let rec FactorialBigInteger(n:int): bigint =  
    match n with  
    | 1 -> bigint(1)  
    | n -> bigint(n) * FactorialBigInteger(n - 1)  
  
let timer:Stopwatch = new Stopwatch()  
let mutable facIntResult:int64 = int64(0)  
let mutable facDblResult:double = double(0)  
let mutable facBigResult:bigint = bigint(0)  
let mutable i:int = 0  
  
let values:list<int> = [5..5..50]  
  
printfn "\nFactorial using Int64"  
// Benchmark Factorial using Int64  
for i in values do  
    timer.Start();    
    facIntResult <- FactorialInt64(i)  
    timer.Stop();   
    printfn "(%d) = %s : %s" i (timer.Elapsed.ToString()) (facIntResult.ToString())  
  
printfn "\nFactorial using Double"  
// Benchmark Factorial using Double  
for i in values do  
    timer.Start();    
    facDblResult <- FactorialDouble(i)  
    timer.Stop();   
    printfn "(%d) = %s : %s" i (timer.Elapsed.ToString()) (facDblResult.ToString())  
  
printfn "\nFactorial using BigInteger"  
// Benchmark Factorial using Double  
for i in values do  
    timer.Start();  
    facBigResult <- FactorialBigInteger(i)  
    timer.Stop();   
    printfn "(%d) = %s : %s" i (timer.Elapsed.ToString()) (facBigResult.ToString())  
namespace System
namespace System.Diagnostics
namespace System.Numerics
val FactorialInt64 : n:int -> int64

Full name: Script.FactorialInt64
val n : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

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

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)

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

--------------------
type int64 = Int64

Full name: Microsoft.FSharp.Core.int64

--------------------
type int64<'Measure> = int64

Full name: Microsoft.FSharp.Core.int64<_>
val FactorialDouble : n:int -> double

Full name: Script.FactorialDouble
Multiple items
val double : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.double

--------------------
type double = Double

Full name: Microsoft.FSharp.Core.double
val FactorialBigInteger : n:int -> bigint

Full name: Script.FactorialBigInteger
type bigint = BigInteger

Full name: Microsoft.FSharp.Core.bigint
val timer : Stopwatch

Full name: Script.timer
Multiple items
type Stopwatch =
  new : unit -> Stopwatch
  member Elapsed : TimeSpan
  member ElapsedMilliseconds : int64
  member ElapsedTicks : int64
  member IsRunning : bool
  member Reset : unit -> unit
  member Restart : unit -> unit
  member Start : unit -> unit
  member Stop : unit -> unit
  static val Frequency : int64
  ...

Full name: System.Diagnostics.Stopwatch

--------------------
Stopwatch() : unit
val mutable facIntResult : int64

Full name: Script.facIntResult
val mutable facDblResult : double

Full name: Script.facDblResult
val mutable facBigResult : bigint

Full name: Script.facBigResult
val mutable i : int

Full name: Script.i
val values : int list

Full name: Script.values
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
val i : int
Stopwatch.Start() : unit
Stopwatch.Stop() : unit
property Stopwatch.Elapsed: TimeSpan
TimeSpan.ToString() : string
TimeSpan.ToString(format: string) : string
TimeSpan.ToString(format: string, formatProvider: IFormatProvider) : string
Int64.ToString() : string
Int64.ToString(format: string) : string
Int64.ToString(provider: IFormatProvider) : string
Int64.ToString(format: string, provider: IFormatProvider) : string
Double.ToString() : string
Double.ToString(provider: IFormatProvider) : string
Double.ToString(format: string) : string
Double.ToString(format: string, provider: IFormatProvider) : string
BigInteger.ToString() : string
BigInteger.ToString(format: string) : string
BigInteger.ToString(provider: IFormatProvider) : string
BigInteger.ToString(format: string, provider: IFormatProvider) : string
Next Version Raw view Test code New version

More information

Link:http://fssnip.net/4n
Posted:7 years ago
Author:Carlos Quintanilla
Tags: recursion , factorial , bigint , timer