12
1
Objective
Take \$a \in ℤ_{>1}\$ and \$b \in ℤ_+\$ as inputs. Write a function \$f\$ such that:
$$ f(a,b) = \left\{ \begin{array}{ll} \log_ab & \quad \text{if} \space \log_ab \in ℚ \\ -1 & \quad \text{otherwise} \end{array} \right. $$
Rules
Types and formats of the inputs doesn't matter.
Type and format of the output doesn't matter either, but the fraction must be irreducible. In C++,
std::pair<int,unsigned>
is an example. (Regarding logarithms, the numerator and the denominator need not to be arbitrary-length integers.)Regardless of whether the input type can handle arbitrary-length integers, the algorithm must be "as if" it were handling those. So using floating-point numbers is implicitly banned.
If \$a\$ and \$b\$ aren't in the sets as specified above, the challenge falls in don't care situation.
\$f\$ may be curried or uncurried.
As this is a code-golf, the shortest code in bytes wins.
Test cases
a b => answer
-----------------
2 8 => 3/1
3 9 => 2/1
8 2 => 1/3
9 3 => 1/2
4 8 => 3/2
64 256 => 4/3
999 1 => 0/1
-----------------
2 7 => -1
10 50 => -1
5 15 => -1
5Could you add some example test cases? Preferably ones where $log_ab \in \mathbb{Q} \setminus \mathbb{Z}$ – Value Ink – 2020-02-13T23:16:24.087
3@PostRockGarfHunter One example is $ \log_4 8 = 3/2 $. – Bubbler – 2020-02-13T23:19:23.180
they must be able to represent arbitrary-length integers
This is a rather odd requirement, since it unfairly penalizes languages which don't have access to arbitrary-precision integer type (many of them don't even have a thing called "import"). Instead, we usually say "use the most natural number type for the language, but the underlying algorithm should generalize to higher numbers." – Bubbler – 2020-02-13T23:35:40.660@Bubbler I added that requirement to implicitly ban floating-point numbers, but to think about it, that's better. – Dannyu NDos – 2020-02-13T23:38:23.123
7
It looks like you posted this from the Sandbox with edits that are only in the live version, which is unfortunate. Had you waited, I would have pointed out that you still don't have any test cases, which I had commented to remind you to add. And Bubbler might have raised their point about integer size in the Sandbox, which would give you more time to think how to address it, and I might have asked to clarify your new "as if" rule, which I think is unclear as it stands. See Things to avoid: Assuming you've addressed sandbox feedback.
– xnor – 2020-02-13T23:59:15.107@xnor Well... You know what is more unfortunate? The Mathematica answer apparently use builtin exact arithmetic. This is not what I intended. To admit it, I didn't even want a code golf. I just wanted to see some art of integer arithmetics. So farewell, I'm closing this challenge. – Dannyu NDos – 2020-02-14T00:16:17.107
Do we have to output as a fraction or can we output as a float? – Lyxal – 2020-02-14T00:18:36.950