# Nth Root in Ruby

**URL:** <https://rubytalk.org/t/nth-root-in-ruby/41578>\
**Category:** ruby-talk\
**Created:** [22 October 2007 17:26 UTC](https://rubytalk.org/t/nth-root-in-ruby/41578 "2007-10-22T17:26:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Laurens](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@Peter\_Laurens](https://rubytalk.org/u/Peter_Laurens)\
**Post date:** [22 October 2007 17:26 UTC](https://rubytalk.org/t/nth-root-in-ruby/41578/1 "2007-10-22T17:26:52Z")

</div>

Hi,

I have tried Googling, but am finding it difficult to track down an  
answer to my question:

What's the easiest way to calculate the Nth root of a number in Ruby,  
I've looked at the Math module but this doesn't seem to include this  
functionality unless I'm missing something. Do I need to roll my own?

Thanks,

- N

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![A\_LeDonne](https://avatars.discourse-cdn.com/v4/letter/a/6bbea6/32.png) [@A\_LeDonne](https://rubytalk.org/u/A_LeDonne)\
**Post date:** [22 October 2007 17:32 UTC](https://rubytalk.org/t/nth-root-in-ruby/41578/2 "2007-10-22T17:32:54Z")

</div>

Try raising to the power of the reciprocal:

irb(main):001:0\> # calculate a third root  
irb(main):002:0\> 10.0 \*\* (1.0/3.0)  
=\> 2.15443469003188

-A

> **···**
>
> On 10/22/07, Peter Laurens \<peterlaurenspublic@gmail.com\> wrote:
> 
> > Hi,
> > 
> > I have tried Googling, but am finding it difficult to track down an  
> > answer to my question:
> > 
> > What's the easiest way to calculate the Nth root of a number in Ruby,  
> > I've looked at the Math module but this doesn't seem to include this  
> > functionality unless I'm missing something. Do I need to roll my own?
> > 
> > Thanks,
> > 
> > - N

---

<div class="post-metadata">

**Author:** ![Ron\_Fox](https://avatars.discourse-cdn.com/v4/letter/r/e8c25b/32.png) [@Ron\_Fox](https://rubytalk.org/u/Ron_Fox)\
**Post date:** [22 October 2007 17:40 UTC](https://rubytalk.org/t/nth-root-in-ruby/41578/3 "2007-10-22T17:40:03Z")

</div>

def rootn(x,n)  
&nbsp;&nbsp;&nbsp;Math.exp(Math.log(x)/n)  
end

Probably should check for X \> 0 and do appropriate  
special case code if not.

RF

Peter Laurens wrote:

> **···**
>
> > Hi,
> > 
> > I have tried Googling, but am finding it difficult to track down an  
> > answer to my question:
> > 
> > What's the easiest way to calculate the Nth root of a number in Ruby,  
> > I've looked at the Math module but this doesn't seem to include this  
> > functionality unless I'm missing something. Do I need to roll my own?
> > 
> > Thanks,
> > 
> > - N

---

<div class="post-metadata">

**Author:** ![Peter\_Laurens](https://avatars.discourse-cdn.com/v4/letter/p/9fc29f/32.png) [@Peter\_Laurens](https://rubytalk.org/u/Peter_Laurens)\
**Post date:** [22 October 2007 17:35 UTC](https://rubytalk.org/t/nth-root-in-ruby/41578/4 "2007-10-22T17:35:13Z")

</div>

Alex LeDonne wrote:

> **···**
>
> > On 10/22/07, Peter Laurens \<peterlaurenspublic@gmail.com\> wrote:
> > 
> > > - N
> > 
> > Try raising to the power of the reciprocal:
> > 
> > irb(main):001:0\> # calculate a third root  
> > irb(main):002:0\> 10.0 \*\* (1.0/3.0)  
> > =\> 2.15443469003188
> > 
> > -A
> 
> Of course - thanks.  
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
