find the nth root of a number in ruby

To find the nth root of a number in Ruby, you can use the exponentiation operator (**) and the **(1/n) syntax to calculate the nth root. Here's an example code snippet to find the cube root of a number:

main.rb
number = 64
n = 3
cuberoot = number ** (1.0 / n)
puts "The cube root of #{number} is #{cuberoot}"
98 chars
5 lines

In this example, we first define the number and n variables to represent the input number and the desired root, respectively. Then, we calculate the cuberoot of number by raising it to the power of 1/n, where n is 3 for the cube root. Finally, we use puts to display the result in a string. You can change the values of number and n to find the nth root of any other number.

gistlibby LogSnag