其他分享
首页 > 其他分享> > big.Int阶乘

big.Int阶乘

作者:互联网

package main

import (
	"fmt"
	"math/big"
	"sync"
	"time"
)

//var myMap = make(map[uint]uint, 10)
//var lock sync.Mutex

var synMap sync.Map

func test(n *big.Int) {
	res := big.NewInt(1)
	for i := big.NewInt(1); i.Cmp(n) == -1; i.Add(i, big.NewInt(1)) {
		res.Mul(i, res)
		//fmt.Printf("i值为:%v,n为%v,res为 %v\n", i,n, res)
	}
	//lock.Lock()
	//myMap[n] = res
	//lock.Unlock()
	if n.Sub(n,big.NewInt(1)).Cmp(big.NewInt(0))==1{
		synMap.Store(n, res)
	}
}

func normal(n uint) {
	var res uint = 1
	var i uint = 1
	for ; i <= n; i++ {
		res *= i
	}

	//lock.Lock()
	//myMap[n] = res
	//lock.Unlock()
	synMap.Store(n, res)
}

func main() {

	var i uint = 1
	for ; i < 200; i++ {
		//go normal(i)
		go test(big.NewInt(int64(i)))
	}
	time.Sleep(time.Second)
	synMap.Range(func(k, v interface{}) bool {
		fmt.Print(k)
		fmt.Print(":")
		fmt.Printf("%v", v)
		fmt.Println()
		return true
	})

	//for k, v := range myMap {
	//	fmt.Printf("map[%d]=%d\n", k, v)
	//}

}

标签:NewInt,Int,res,fmt,var,uint,big,阶乘
来源: https://blog.csdn.net/weixin_42248522/article/details/112852056