I have been trying to figure out how to programmatically find a square root of a number in Swift. I am looking for the simplest possible way to accomplish with as little code ne
this should work for any root, 2 - ∞, but you probably don't care:
func root(input: Double, base: Int = 2) -> Double {
var output = 0.0
var add = 0.0
while add < 16.0 {
while pow(output, base) <= input {
output += pow(10.0, (-1.0 * add))
}
output -= pow(10.0, (-1.0 * add))
add += 1.0
}
return output + 0.0
}