Sayuj
Sayuj

Reputation: 7622

Ruby: Find exponent of 10

x ^ y = z I have value for x and z. I want to find out value of y using Ruby.

For example:

x = 10
z = 100
# 10 ^ 2 => 100

My expected result is 2. Is there any inbuilt method in Ruby language to figure out this?

Upvotes: 0

Views: 239

Answers (2)

Thomas
Thomas

Reputation: 182000

The inverse of exponentiation is called the logarithm. In Ruby, the base-10 logarithm (i.e. x == 10) is implemented as Math.log10(z):

irb(main):005:0> Math.log10(100)
=> 2.0

If you need it for different values of x, use Math.log(z, x):

irb(main):006:0> Math.log(100, 10)
=> 2.0

Upvotes: 5

mrzasa
mrzasa

Reputation: 23327

It's logarithmic function:

> Math.log(100, 10)
=> 2.0

Upvotes: 3

Related Questions