Skip to content

Instantly share code, notes, and snippets.

@erikerlandson
Last active August 29, 2015 14:04
Show Gist options
  • Save erikerlandson/ade02c1c248a79e87e39 to your computer and use it in GitHub Desktop.
Save erikerlandson/ade02c1c248a79e87e39 to your computer and use it in GitHub Desktop.
scala power function
def pow(n: Int, k:Int): Int = k match {
case kk if kk < 0 => 0 // error
case 0 => 1
case 1 => n
case 2 => n*n
case kk if kk % 2 == 0 => pow(pow(n, k/2), 2)
case _ => pow(n, k/2)*pow(n, (k+1)/2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment