mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
Factorial doesn't accept negative numbers
Previously factorial could accept negative number which isn't defined
This commit is contained in:

committed by
Alexander Medvednikov

parent
ebbd63811d
commit
801e06318f
@ -223,9 +223,12 @@ pub fn trunc(a f64) f64 {
|
|||||||
|
|
||||||
// factorial calculates the factorial of the provided value.
|
// factorial calculates the factorial of the provided value.
|
||||||
pub fn factorial(a int) i64 {
|
pub fn factorial(a int) i64 {
|
||||||
|
if a < 0 {
|
||||||
|
panic('factorial: Cannot find factorial of negative number')
|
||||||
|
}
|
||||||
mut prod := 1
|
mut prod := 1
|
||||||
for i:= 0; i < a; i++ {
|
for i:= 0; i < a; i++ {
|
||||||
prod = prod * (i+1)
|
prod *= (i+1)
|
||||||
}
|
}
|
||||||
return prod
|
return prod
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user