Skip to content

Instantly share code, notes, and snippets.

@erikerlandson
Created August 6, 2014 18:58
Show Gist options
  • Save erikerlandson/02d300db0e9f3f1870c2 to your computer and use it in GitHub Desktop.
Save erikerlandson/02d300db0e9f3f1870c2 to your computer and use it in GitHub Desktop.
tail recursive power function in scala
import scala.annotation.tailrec
def pow(n: Int, k: Int): Int = {
if (k <= 0) return 1
@tailrec def p(acc: Int, k:Int):Int = if (k == 1) acc else p(n*acc, k-1)
p(n, k)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment