Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@CheolhoJeon
Created May 8, 2021 08:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CheolhoJeon/9b3c70b4de85412e8b93a175b391d83b to your computer and use it in GitHub Desktop.
Save CheolhoJeon/9b3c70b4de85412e8b93a175b391d83b to your computer and use it in GitHub Desktop.
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