1 2 3 4 5 6 7 8 9 10
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) }
You can clone a snippet to your computer for local editing. Learn more.