# Question about repeating decimal?

For simple fraction, we can easily convert it to repeating decimal by calculator. Ex. $\frac 1 3 = 0.33333\ldots$, $\frac 1 7=0.(142857),\ldots$ But some fraction fraction like $10/29, 1/97,...$ The repeating part of them are too long, so it can't fully show on the calculator. So is there any algorithm to find the repeating part for that fraction?

• Doing the division by hand until finding the first repeated remainder. The repeating part can be arbitrarily long and appear after an arbitrarily long initial part, so every computer capacity can be exceeded. Mar 22, 2015 at 13:45
• Possible duplicate of Convert a fraction to infinite repeating decimal? Jul 25, 2018 at 12:40

## 3 Answers

Yes, there is. This wikipedia page explains the various algorithms in the Section "Converting repeating decimals to fractions".

Yes. Here is one I once wrote in the R language. It uses the 'gmp' (GNU Multiple Precision) package.

#########################################################
### Functions for converting back and forth between   ###
### rational numbers and decimals. Repeating decimals ###
### are denoted by brackets around the digit block    ###
### repeats. For example, 11/6 is 1.8[3] in decimal.  ###
#########################################################

#############################
### Author: Roman Chokler ###
#############################
library(gmp)

bigq.to.dec <- function(q)
{
sgn <- sign(q)
num <- abs(numerator(q))
den <- denominator(q)
d <- den
c2 <- 0
c5 <- 0
while(d %% 2==0)
{
d <- as.bigz(d/2)
c2 <- c2 + 1
}
while(d %% 5==0)
{
d <- as.bigz(d/5)
c5 <- c5 + 1
}
transient <- max(c2,c5)
dec <- as.character(as.bigz(num/den))
rem <- num %% den
if (rem != 0)
{
dec <- paste0(dec,".")
if (transient>0)
{
for(i in 1:transient)
{
rem <- rem * 10
dec <- paste0(dec,as.bigz(rem/den))
rem <- rem %% den
}
}
if (rem != 0)
{
dec <- paste0(dec,"[")
r <- rem
rem <- rem * 10
dec <- paste0(dec,as.bigz(rem/den))
rem <- rem %% den
while (rem != r)
{
rem <- rem * 10
dec <- paste0(dec,as.bigz(rem/den))
rem <- rem %% den
}
dec <- paste0(dec,"]")
}
}
if (sgn == -1)
{
dec <- paste0("-",dec)
}
return(dec)
}

dec.to.bigq <- function(dec)
{
bq <- 0
sgn <- 1
n <- nchar(dec)
if (regexpr("-",dec)[1]==1)
{
sgn <- -1
dec <- substr(dec,2,n)
n <- n - 1
}
pnt <- regexpr("\\.",dec)[1]
rstart <- regexpr("\$",dec)[1] rend <- regexpr("\$",dec)[1]
if (pnt == -1)
{
return(as.bigq(dec) * sgn)
}
if (n==pnt)
{
return((as.bigq(substr(dec,1,pnt-1))) * sgn)
}
bq <- as.bigq(substr(dec,1,pnt-1))
if (rstart == -1)
{
transient <- substr(dec,pnt+1,n)
den <- pow.bigz(10,nchar(transient))
transient <- sub("^0+","",transient)
if (transient == "")
{
transient <- "0"
}
return((bq + div.bigq(transient,den)) * sgn)
}
den <- as.bigz(1)
if (rstart > pnt + 1)
{
transient <- substr(dec,pnt+1,rstart-1)
den <- pow.bigz(10,nchar(transient))
transient <- sub("^0+","",transient)
if (transient == "")
{
transient <- "0"
}
bq <- bq + div.bigq(transient,den)
}
if ((rend == -1) || (rstart >= rend - 1) || n > rend)
{
return(as.bigq(NA))
}
rep <- substr(dec,rstart+1,rend-1)
den <- den * (pow.bigz(10,nchar(rep)) - 1)
rep <- sub("^0+","",rep)
if (rep == "")
{
rep <- "0"
}
return((bq + div.bigq(rep,den)) * sgn)
}


For, say, $$\large \frac{1}{97}$$, start by running the Python program

R = []

numer = 1
denom = 97

for i in range(0,denom):
r = numer * 10**i % denom
try:
ndx = R.index(r)
except:
ndx = -1
if ndx >= 0:
print(i,ndx)
break
R.append(r)


The output of the program is

96 0


The period length is

i - ndx


and is therefore equal to $$96 - 0 = 96$$.

The offset (to the right of the decimal) is $$0$$ so the period begins immediately after the decimal.

So we need to put a bar over the first $$96$$ digits.

To get those decimal digits you can conveniently use wolfram, and compute

integerPart((1 + 1/97) * 10^96)


The output is

1010309278350515463917525773195876288659793814432989690721649484536082474226804123711340206185567


and you have to drop the leading $$1$$ (used to 'light up' any zeroes right after the decimal point) to get the final answer (with period broken down into blocks of $$25$$),

$$\quad \large \frac{1}{97} =$$

$$0.\overline{0103092783505154639175257}$$
$$\; \; \, \overline{7319587628865979381443298}$$
$$\; \; \, \overline{9690721649484536082474226}$$
$$\; \; \, \overline{804123711340206185567}$$