Files
sjy01-image-proc/vendor/github.com/nuknal/goNum/Factorial.go
2024-10-24 15:46:01 +08:00

37 lines
806 B
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Factorial
/*
------------------------------------------------------
作者 : Black Ghost
日期 : 2018-12-6
版本 : 0.0.0
------------------------------------------------------
计算自然数n的阶乘
------------------------------------------------------
输入 :
n 自然数
输出 :
sol 阶乘结果
err 解出标志false-未解出或达到步数上限;
true-全部解出
------------------------------------------------------
*/
package goNum
// Factorial 计算自然数n的阶乘
func Factorial(n int) int {
//判断n
if n < 0 {
panic("Error in goNum.Factorial: n < 1")
}
if n == 0 {
return 1
}
//计算
var sol int = 1
for i := n; i > 1; i-- {
sol = sol * i
}
return sol
}