The decimal extension program is very slow for large inputs

I am writing a program to calculate the decimal extension by a number 103993/33102, and I want to print all the final decimal numbers depending on which number the user enters. It works fast for all numbers before 10^5, but if 10^6it takes about 5 minutes to enter a response to the program. How can I speed up the process? I tried using two different approaches using BigDecimal, and the other using strings, and none of them work efficiently.

public static void main(String[] args) throws NumberFormatException,
        IOException {
    // BigDecimal num1 = new BigDecimal(103993);
    // BigDecimal num2 = new BigDecimal(33102);
    String repNum = "415926530119026040722614947737296840070086399613316";
    // pw.println(num.toString());
    String sNum = "3.1";
    // pw.println(repNum.length());
    int cases = Integer.parseInt(br.readLine());
    int dec;
    for (int i = 0; i < cases; i++) {
        sNum = "3.1";
        dec = Integer.parseInt(br.readLine());

        if (dec == 0)
            pw.println("3");
        else if (dec <= 52) {
            sNum += repNum.substring(0, dec - 1);
            pw.println(sNum);
        } else {
            while (dec > 52) {
                sNum += repNum;
                dec -= 51;
            }
            sNum += repNum.substring(0, dec - 1);
            pw.println(sNum);

        }

        // pw.println(num1.divide(num2, dec,
        // RoundingMode.FLOOR).toString());
    }
}
+5
source share
1 answer

Instead of creating a long string of numbers, just print the numbers. For instance:

        while (dec > 52) {
            System.out.print(repNum);
            dec -= 51;
        }
        pw.println(repNum.substring(0, dec - 1));

, . , , , , Schlemiel the Painter.

+2

All Articles