Skip to content

Instantly share code, notes, and snippets.

Avatar
🙏
Eating Kotlin 🍰

Cheolho Jeon CheolhoJeon

🙏
Eating Kotlin 🍰
View GitHub Profile
View Fibonacci.kt
package chap4.Recursion
import atomictest.eq
fun fibonacci(n: Int): Long {
tailrec fun fibonacci(
n: Int,
current: Long,
next: Long
): Long {
View fibonacci.kt
package chap4.Recursion
import atomictest.eq
fun fibonacci(n: Long): Long {
return when (n) {
0L -> 0
1L -> 1
else ->
fibonacci(n - 1) + fibonacci(n - 2)
View fibonacci.kt
package chap4.Recursion
fun fibonacci(n: Long): Long {
return when (n) {
0L -> 0
1L -> 1
else ->
fibonacci(n - 1) + fibonacci(n - 2)
}
}
View TailRecursiveSum.kt
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)
View Iteration.kt
package chap4.Recursion.iteration
import atomictest.eq
fun sum(n: Long): Long {
var accumulator = 0L
for (i in 1..n) {
accumulator += i
}
return accumulator
View RecursionLimits.kt
package chap4.Recursion
import atomictest.eq
fun sum(n: Long): Long {
if (n == 0L) return 0
return n + sum(n - 1)
}
fun main() {
View InfiniteRecursion.kt
package chap4.Recursion
fun recurse(i: Int): Int = recurse(i + 1)
fun main() {
// println(recurse(1))
}
View InfiniteRecursion.kt
package chap4.Recursion
fun recurse(i: Int): Int = recurse(i + 1)
fun main() {
println(recurse(1))
}
View CallStack.kt
package chap4.Recursion
import java.lang.IllegalStateException
fun illegalState() {
throw IllegalStateException()
}
fun fail() = illegalState()
View Factorial.kt
package chap4.Recursion
import atomictest.eq
fun factorial(n: Long): Long {
if (n <= 1) return 1
return n * factorial(n - 1)
}
fun main() {