A ripoff of this challenge. Go upvote it!
Objective
Given a rational number amongst \$[0,1]\$, apply the Cantor function to it and output the rational number that's produced.
The Cantor function
The Cantor function is continuous everywhere and constant almost everywhere, but has an average slope of 1:
The Cantor function \$f(x)\$ can be expressed as a limit \$f(x)=\lim\limits_{n\to\infty} f_n(x)\$ of a sequence of functions \$f_0, f_1, f_2, \dots\$, which are defined recursively as:
\$f_0(x)=x\$
\$f_{n+1}(x)=\left\{\begin{matrix}\frac{1}{2}f_n(3x)&x\in[0,\frac{1}{3})\\ \frac{1}{2}&x\in[\frac{1}{3},\frac{2}{3})\\ \frac{1}{2}+\frac{1}{2}f_n(3x-2)&x\in[\frac{2}{3},1] \end{matrix}\right.\$
Your task is to compute \$f(x)\$ for the rational \$x \in [0,1] \cap \mathbb{Q}\$ given as input.
How?
Though this task might seem impossible, this is actually possible, for the Cantor function is computable.
A step-by-step solution for \$x \in \mathbb{R}\$:
Ternary-expand \$x\$ to \$0.t_1t_2t_3\cdots\$.
Write "0.".
Set \$n=1\$.
If \$t_n = 1\$, write "1" and halt.
Otherwise, if \$t_n = 0\$, write "0", increment \$n\$, then continue doing step #4.
Otherwise (\$t_n = 2\$), write "1", increment \$n\$, then continue doing step #4.
- Parse the resulting string as a binary expansion of a real number.
As \$x\$ actually is in \$\mathbb{Q}\$ in this challenge, you should exploit the fact that the ternary expansion of \$x\$ repeats. It follows that the output is also in \$\mathbb{Q}\$.
Examples
$$ \begin{array}{r|c|c|c|c|c|c|c|c|c|c} x & 0 & ½ & ⅓ & ¼ & ⅕ & ⅚ & 1 \\ \hline \text{Ternary expansion of }x & 0.\overline{0} & 0.\overline{1} & 0.1\overline{0} & 0.\overline{02} & 0.\overline{0121} & 0.2\overline{1} & 0.\overline{2} \\ \hline \text{Binary expansion of } f(x) & 0.\overline{0} & 0.1 & 0.1 & 0.\overline{01} & 0.01 & 0.11 & 0.\overline{1} \\ \hline f(x) & 0 & ½ & ½ & ⅓ & ¼ & ¾ & 1 \end{array} $$
Rules
Invalid inputs fall in don't care situation. In particular, you don't need to deal with numbers outside of \$[0,1]\$.
Input and output must be exact rational numbers. If your language doesn't natively support rational number arithmetic, use a pair of integers.
1/x
. For instance:1/787
~>7/512
. \$\endgroup\$