When you divide two whole numbers the result has an infinite number of decimals if and only if the denominator of the simplified fraction is a multiple of some prime number other than 2 and 5.
But all divisions of integers with infinite decimals have something in common, there comes a time when they are repeated. And it is possible to represent all the infinite decimals by putting the initial part that is not repeated followed by the one that repeats itself.
For example:
Which means that the group of 6 figures under the arch is repeated infinitely.
This is calculated by storing the dividends from the decimal point, and when they are repeated you have found the cycle. We also store the quotients, which will be the result.
1.- 6 entre 7 es 0 y sobran 6, multiplico el resto por 10 para el siguiente decimal
2.- 60 entre 7 es 8 y sobran 4, multiplico el resto por 10 para el siguiente decimal
3.- 40 entre 7 es 5 y sobran 5, multiplico el resto por 10 para el siguiente decimal
4.- 50 entre 7 es 7 y sobra 1, multiplico el resto por 10 para el siguiente decimal
5.- 10 entre 7 es 1 y sobran 3, multiplico el resto por 10 para el siguiente decimal
6.- 30 entre 7 es 4 y sobran 2, multiplico el resto por 10 para el siguiente decimal
7.- 20 entre 7 es 2 y sobran 6, multiplico el resto por 10 para el siguiente decimal
8.- 60 entre 7 .....
60! it's repeated. We have found our cycle. This is from steps 2 (inclusive) to 8 (not inclusive)
And now the same in java.
import java.util.*;
public class DecimalesInfinitos {
public static void main(String[] args) {
division(6,7);
division(73,30);
division(2,149);
division(1,14911941);
division(1,141140);
}
public static void division(long dividendo, long divisor) {
assert( dividendo>=0 && divisor>0 );
System.out.println( "" + dividendo + "/" + divisor + "=" );
// Primero la parte entera
long cocienteEntero = dividendo/divisor;
// Ahora la parte decimal
dividendo %= divisor;
List<Long> listaDividendos = new ArrayList<>();
List<Long> listaCocientes = new ArrayList<>();
dividendo *= 10;
while(!listaDividendos.contains( dividendo )) {
if ( dividendo==0 ) {
imprimeNumeroSinInfinitosDecimales(
cocienteEntero, listaCocientes);
return;
}
listaDividendos.add( dividendo );
listaCocientes .add( dividendo / divisor );
dividendo %= divisor;
dividendo *= 10;
}
// Localizar donde empieza la parte periódica
long indiceInicioPeriodo = listaDividendos.indexOf(dividendo);
// Imprimimos la parte entera y el separador decimal
System.out.print( cocienteEntero );
System.out.print( "'");
// Imprimir la parte no periódica
int i = 0;
for (; i<indiceInicioPeriodo; ++i)
System.out.print( listaCocientes.get(i).longValue() );
// Imprimir un subrayado para indicar el inicio de la parte periódica
System.out.print("_");
// Imprimir la parte periódica
for (; i<listaCocientes.size(); ++i )
System.out.print( listaCocientes.get(i).longValue() );
// Y unos puntitos para indicar que se repiten
System.out.println("...");
}
public static void imprimeNumeroSinInfinitosDecimales(
long cocienteEntero, List<Long> listaCocientes) {
System.out.print( cocienteEntero );
System.out.print( "'");
for ( long n : listaCocientes )
System.out.print(n);
System.out.println("");
}
}
What gives us as a result:
6/7 =
0'_857142 ...
73/30 =
2'4_3 ...
2/149 =
0'_0134228187919463087248322147651006711409395973154362416107382550335570469798657718120805369127516778523489932885906040268456375838926174496644295302 ...
1/14911941 =
1/141140 = (I omit these two results because OS allows only 30,000 characters)
I show the results directly on the screen instead of in a StringBuffer or String. Adapting it to store them in a StringBuffer or String is an exercise for the reader.