```
package main
import (
"fmt"
"math"
)
//获取阶乘的位数
func Get(number int) int {
sum := 1.0
for i := 1; i < number+1; i++ {
sum += math.Log10(float64(i))
}
return int(sum)
}
//初始化结果数组
func InitArray(length int) []int {
array := make([]int, length)
array[0] = 1
return array
}
//开始计算阶乘的值
func Getvalue(number int) ([]int, int) {
length := Get(number)
array := InitArray(length)
position := 0
for i := 2; i <= number; i++ { //2--number逐个与原来的结果相乘
for j := length - 1; j >= 0; j-- {
if array[j] != 0 {
position = j
break
}
}
for k := 0; k <= position; k++ {
array[k] *= i
}
//乘完一轮后进行进位处理
Deal(array, position)
}
position = Index(array, length-1)
return array, position
}
//进位处理
func Deal(array []int, position int) {
value := 0 //进位值
for i := 0; i <= position; i++ { //0--pos逐个检查是否要进位
array[i] += value
if array[i] <= 9 {
value = 0
} else if array[i] > 9 && i < position { //需进位,但未达到最高位
value = array[i] / 10
array[i] = array[i] % 10
} else {
for array[i] > 9 { //循环向前进位
value = array[i] / 10
array[i] = array[i] % 10
i++
array[i] = value
}
}
}
return
}
//最高位的下标
func Index(array []int, length int) int {
for j := length; j >= 0; j-- {
if array[j] != 0 {
position := j
return position
}
}
return 0
}
func main() {
array, position := Getvalue(100)
fmt.Printf("100!的长度%d\n", len(array))
for i := position; i >= 0; i-- {
fmt.Printf("%d", array[i])
}
}
```
有疑问加站长微信联系(非本文作者)