elect
05/21/2020, 1:01 PMtailrect
function?
var result = 1
for (i in 1..a)
result *= i
result
Mohsen Kashi
05/21/2020, 1:33 PMelect
05/21/2020, 1:40 PMMohsen Kashi
05/21/2020, 1:41 PMmarstran
05/21/2020, 6:15 PMLong
so quickly that it doesn't make that much sense to do it. You cannot get a StackOverflowException
with it though.
fun factorial(n: Long): Long {
tailrec fun fact(acc: Long, curr: Long): Long =
if (curr == 1L) acc
else fact(acc * curr, curr - 1L)
return fact(1, n)
}
serebit
05/21/2020, 10:44 PMreduce
instead, like (1..a).reduce { product, next -> product * next }