TheBulgarianWolf

Kotlin Recursion

Mar 23rd, 2021
717
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.math.BigInteger
  2.  
  3. fun main(args: Array<String>) {
  4.     val number = 4
  5.     val result: Long
  6.  
  7.     result = factorial(number)
  8.     println("Factorial of $number = $result")
  9.     val n = 100
  10.     val first = BigInteger("0")
  11.     val second = BigInteger("1")
  12.  
  13.     println(fibonacci(n, first, second))
  14. }
  15.  
  16. fun factorial(n: Int): Long {
  17.     return if (n == 1) n.toLong() else n*factorial(n-1)
  18. }
  19.  
  20. tailrec fun fibonacci(n: Int, a: BigInteger, b: BigInteger): BigInteger {
  21.     return if (n == 0) a else fibonacci(n-1, b, a+b)
  22. }
RAW Paste Data