2019-10-29 22:46:27 +03:00
|
|
|
package profile
|
|
|
|
|
|
|
|
import (
|
2020-05-06 12:23:37 +03:00
|
|
|
"log"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
2019-10-29 22:46:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func Duration(invocation time.Time, name string) {
|
2020-05-06 12:23:37 +03:00
|
|
|
elapsed := time.Since(invocation)
|
2019-10-29 22:46:27 +03:00
|
|
|
|
2020-05-06 12:23:37 +03:00
|
|
|
log.Printf("%s lasted %s", name, elapsed)
|
2019-10-29 22:46:27 +03:00
|
|
|
}
|
2019-10-29 22:48:41 +03:00
|
|
|
|
|
|
|
func BigIntFactorial(x big.Int) *big.Int {
|
2020-05-06 12:23:37 +03:00
|
|
|
// 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")
|
2019-10-29 22:48:41 +03:00
|
|
|
|
2020-05-06 12:23:37 +03:00
|
|
|
y := big.NewInt(1)
|
2020-05-27 16:53:18 +03:00
|
|
|
for one := big.NewInt(1); x.Sign() > 0; x.Sub(&x, one) {
|
|
|
|
y.Mul(y, &x)
|
2020-05-06 12:23:37 +03:00
|
|
|
}
|
2019-10-29 22:48:41 +03:00
|
|
|
|
2020-05-06 12:23:37 +03:00
|
|
|
return x.Set(y)
|
2019-10-29 22:48:41 +03:00
|
|
|
}
|