Repeated reciprocal
Python 3, 101 bytes
lambda s:g(int(s.replace(".","")),10**s[::-1].index("."))
g=lambda a,b:a and(b%a and g(b%a,a)or b//a)
Try it online!
Format: the string must contain a decimal point.
Mathematica, 36 bytes
Last@*ContinuedFraction@*Rationalize
Demo
In[1]:= f = Last@*ContinuedFraction@*Rationalize
Out[1]= Last @* ContinuedFraction @* Rationalize
In[2]:= f[0]
Out[2]= 0
In[3]:= f[0.1]
Out[3]= 10
In[4]:= f[0.2]
Out[4]= 5
In[5]:= f[0.3]
Out[5]= 3
In[6]:= f[0.4]
Out[6]= 2
In[7]:= f[0.5]
Out[7]= 2
In[8]:= f[0.6]
Out[8]= 2
In[9]:= f[0.7]
Out[9]= 3
In[10]:= f[0.8]
Out[10]= 4
In[11]:= f[0.9]
Out[11]= 9
In[12]:= f[1]
Out[12]= 1
J, 18 bytes
%@(-<.)^:(~:<.)^:_
In J, the idiom u ^: v ^:_
means "Keep applying the verb u
while condition v
returns true.
In our case, the ending condition is defined by the hook ~:<.
, which means "the floor of the number <.
is not equal ~:
to the number itself" -- so we'll stop when the main verb u
returns an int.
u
in this case is another hook -<.
-- the number minus its floor -- whose return value is fed into @
the reciprocal verb %
.
Try it online!