find the nth root of a number in swift

To find the nth root of a number in Swift, you can use the pow() function to raise the number to the power of 1/n. Here's an example code snippet:

main.swift
let number = 27.0
let n = 3

let result = pow(number, 1.0/Double(n))
print(result) // output: 3.0
98 chars
6 lines

In this example, we first define the number and the value of n (which represents the nth root we want to find). We then compute the result by calling the pow() function and passing in the number as the first argument, and the expression 1.0/Double(n) as the second argument. This expression evaluates to the fraction 1/n, which is used as the exponent to raise the number to. Finally, we print the result, which in this case is the cube root of 27.0, or 3.0.

gistlibby LogSnag