Last active
March 6, 2019 19:50
-
-
Save rezaiyan/e246854ad1de2b2c4dc1b90cd3a10717 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun factorial(number: Int): Int { | |
var factor = 1 | |
return if (number <= 1) | |
factor | |
else { | |
(1 until number).forEach { factor*=(it+1) } | |
factor | |
} | |
} | |
fun factorialRec(number: Int): Int { | |
return if (number<=1) | |
1 | |
else number * factorialRec(number-1) | |
} | |
tailrec fun factorialTaiRec(number: Int, accumulator:Int):Int{ | |
return if (number==0) | |
accumulator | |
else factorialTaiRec(number-1,number*accumulator) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment