<div dir="ltr"><div>After thinking this through a bit, I guess the algorithm I suggested is as fast as it goes, as because it's a rational, the actual result decimal digits are *not* available plaintext in the number's internal structures or alike, so this is the fastest algorithm. If you have any other thought on this feel free to tell.</div>

<div><br></div><div>I believe you addressed now how to detect whether a rational has inifinite decimals - yes I got aware there's an algorithm to do this too, (even while it's iterative - would be nice to understand how fast it is, anyhow) neat.</div>

<div><br></div><div>I agree with you fully that being exposed to challenging questions at every age does good to the soul.</div><div><br></div><div>Mikael</div><div><br></div><br><div><br></div></div><div class="gmail_extra">

<br><br><div class="gmail_quote">2014/1/15 Bradley Lucier <span dir="ltr"><<a href="mailto:lucier@math.purdue.edu" target="_blank">lucier@math.purdue.edu</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="im">On 01/15/2014 12:58 PM, Mikael wrote:<br>
> Ah, actually this can be implemented for #e12345.6789 by first<br>
> flooring and printing that out ("12345") then deducting that from the<br>
> value (=> 0.6789) and then doing * 10 and |truncate| up to eq? 0 -<br>
> that works. Perhaps it'd even be quite close to optimum speed?<br>
><br>
> I.e. #e12345.6789 (floor #) (- ## #) , and then repeat (eq? # 0) (* ##<br>
> 10) (truncate #) (- ## #).<br>
<br>
</div>Well, this is a mathematical question rather than a programming<br>
question: Are rational numbers and repeating decimals (possibly<br>
repeating 0 at the end) the same kind of numbers?  The answer is yes, of<br>
course.<br>
<br>
To go from repeating decimals, where you have a pattern <pattern> of r<br>
repeating digits at the end, multiply by a power of 10 ($10^k$, say)<br>
such that the digits behind the decimal point are the start of the<br>
repeating pattern, then multiply the number again by $10^{r+k}$:<br>
<br>
10^{r+k}x = integer<pattern>.<pattern><pattern>...<br>
10^kx     =          integer.<pattern><pattern>...<br>
<br>
subtract<br>
<br>
(10^{r+k}-10^k)x=(integer<pattern> - integer)<br>
<br>
so<br>
<br>
x = (integer<pattern> - integer)/(10^{r+k}-10^k)<br>
<br>
To go the other way, if $x=p/q$, divide p.00000000... by q in longhand.<br>
If any of the remainders are 0, then the division ends and 0 is repeated<br>
indefinitely.  Otherwise, there can be only q-1 different remainders, so<br>
eventually one of them will have to repeat; after that the digits in the<br>
quotient repeat indefinitely.<br>
<br>
I'm old enough to have taken the "New Math" in the 1960s, and I remember<br>
my teacher in 6th or 7th grade asking this question.  They asked pretty<br>
sophisticated questions in those days, even if most of the students<br>
didn't really realize it.<br>
<br>
Brad<br>
</blockquote></div><br></div>