func square(value: Int, exponent: Int) -> Int { var result: Int = 1 for _ in 0..<exponent { result *= value } return result }
func power(_ x: Int, _ y: Int) -> Int { if y == 0 { return 1 } else { return x * power(x, y - 1) } }
func pow(_ x: Int, _ y: Int) -> Int { if y == 0 { return 1 } else if y == 1 { return x } else { // compute x^(y/2) let xy2 = pow(x, y / 2) // if y is even if y % 2 == 0 { // x^y is x^(y/2) squared return xy2 * xy2 } else { // x^y is x^(y/2) squared times x return xy2 * xy2 * x } } }