From 7759f2ede9a8ff32830d838b1da5ec9a46968e65 Mon Sep 17 00:00:00 2001 From: YikesHome <31678324+yikeshome@users.noreply.github.com> Date: Wed, 30 Oct 2019 03:48:41 +0800 Subject: [PATCH] Update profile.go --- profiling/profile.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/profiling/profile.go b/profiling/profile.go index f7beae1..e123f45 100644 --- a/profiling/profile.go +++ b/profiling/profile.go @@ -10,3 +10,16 @@ func Duration(invocation time.Time, name string) { log.Printf("%s lasted %s", name, elapsed) } + +func BigIntFactorial(x big.Int) *big.Int { + // Arguments to a defer statement is immediately evaluated and stored. + // The deferred function receives the pre-evaluated values when its invoked. + defer Duration(time.Now(), "IntFactorial") + + y := big.NewInt(1) + for one := big.NewInt(1); x.Sign() > 0; x.Sub(x, one) { + y.Mul(y, x) + } + + return x.Set(y) +}