Last active
October 23, 2023 09:50
-
-
Save sasssass/cceec4c15b1256b73c1b08131e62a651 to your computer and use it in GitHub Desktop.
Factorial.kt
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
// we assume that n is never negative | |
fun calculateFactorialProcedural(n: Int): Int { | |
var factorial = 1 | |
var i = 1 | |
while (i <= n) { | |
factorial *= i | |
i++ | |
} | |
return factorial | |
} | |
fun calculateFactorialStructured(n: Int): Int { | |
return if (n == 0 || n == 1) { | |
1 | |
} else { | |
var factorial = 1 | |
for (i in 2..n) { | |
factorial *= i | |
} | |
factorial | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment