Skip to content

Instantly share code, notes, and snippets.

@CheolhoJeon
Created May 8, 2021 07:21
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/aa6d3ad0c81d573ff379def16c809996 to your computer and use it in GitHub Desktop.
Save CheolhoJeon/aa6d3ad0c81d573ff379def16c809996 to your computer and use it in GitHub Desktop.
package chap4.Recursion
import atomictest.eq
fun sum(n: Long): Long {
if (n == 0L) return 0
return n + sum(n - 1)
}
fun main() {
sum(2) eq 3
sum(1000) eq 500500
// sum(100_000) eq 500050000 // [1]
(1..100_000L).sum() eq 5000050000 // [2]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment