Created
August 6, 2014 18:58
-
-
Save erikerlandson/02d300db0e9f3f1870c2 to your computer and use it in GitHub Desktop.
tail recursive power function in scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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