perform a division and show all the decimals in a String or in a StringBuffer

1

I'm doing a program that divides me and shows me all the decimals of the quotient, I already did it for bigDecimal but it does not show all the decimals I want and I'm trying all the ways but I could not do it as I could do it and show me n decimals regardless of the execution time the number of 'tests are 2/149 and 1/14911941, 1/141140 that have more than 7 thousand decimals or 33 thousand decimals

    
asked by Yeison Osorio 19.04.2017 в 06:19
source

3 answers

2

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.

    
answered by 20.04.2017 в 23:49
0

for when you ask a question, please provide the progress of your code or the error it throws you, to show decimals you have different paths, I show you an example with BigDecimal:

          BigDecimal bg1, bg2, bg3;

          bg1 = new BigDecimal("1");
          bg2 = new BigDecimal("141140");

          bg3 = bg1.divide(bg2, 3500, RoundingMode.CEILING);

          String str = "El resultado es: " +bg3;

          System.out.println( str );

You can also support yourself with setScale

Using DecimalFormat

         double number= 1.42423423423423;
          DecimalFormat df = new DecimalFormat("#.00");
          System.out.println(df.format(number));
          /* Salida : 1.42*/
    
answered by 19.04.2017 в 16:43
0

what you ask for can not be done because each type of variable stores a certain number of bytes, if you want to obtain all decimal places for PI, you would only throw a few.

    
answered by 20.04.2017 в 21:24