Snippets

Chema Cortes Integer pow in scala

Created by Chema Cortes
def potencia(x: Double, n: Int): Double = {
    if (n == 0)
        1
    else if (n < 0)
        1 / potencia(x, -n)
    else if (n % 2 == 0)
        potencia(x, n / 2) * potencia(x, n / 2)
    else
        x * potencia(x, n - 1)
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.