Integer Logarithm

Mathematica, 47 45 43 29 bytes SBCS

If[NumberQ[c=#~Log~#2],c,-1]&

You can try it online!

Thanks to @J42161217 for saving me a total of 16 bytes!!


Python 3, 103 101 97 bytes

Saved 2 bytes thanks to Kevin Cruijssen!!!
Saved 4 bytes thanks to Value Ink!!!

from math import*
def f(a,b):
 l=log(b,a)
 n,m=l.as_integer_ratio()
 return -1if a**n-b**m else l

Try it online!


Charcoal, 88 82 bytes

≔Eθ⟦⟧ζ≔²ηW⊖⌈θ¿⌊﹪θη≦⊕ηUMθ⎇﹪κηκ∧⊞O§ζλη÷κηFζFι⊞υκUMυEζ№λιUMυ÷ι⊟Φ⊕⌈ι∧λ¬⌈﹪ιλ¿›⌈υ⌊υ-1I⊟υ

Try it online! Link is to verbose version of code. Takes input as a list in [value, base] order and outputs numerator and denominator. Edit: Saved 6 bytes by simplifying my check for exclusive factors. Explanation:

≔Eθ⟦⟧ζ≔²ηW⊖⌈θ¿⌊﹪θη≦⊕ηUMθ⎇﹪κηκ∧⊞O§ζλη÷κη

Factorise both numbers by trial division.

FζFι⊞υκ

Collect the factors from both inputs, in case some factors are only present in one of them.

UMυEζ№λιUMυ÷ι⊟Φ⊕⌈ι∧λ¬⌈﹪ιλ

Divide the multiplicity of each factor in both the base and the value by their GCD.

¿›⌈υ⌊υ-1

If this is not unique then output -1.

I⊟υ

If it is unique then output it.