Created
May 8, 2021 08:28
-
-
Save CheolhoJeon/9b3c70b4de85412e8b93a175b391d83b 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
package chap4.Recursion.trailrecursion | |
import atomictest.eq | |
private tailrec fun sum( | |
n: Long, | |
accumulator: Long | |
): Long = | |
if (n == 0L) accumulator | |
else sum(n - 1, accumulator + n) | |
fun sum(n: Long) = sum(n, 0) | |
fun main() { | |
sum(2) eq 3 | |
sum(10000) eq 50005000 | |
sum(100000) eq 5000050000 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment