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 {
String repNum = "415926530119026040722614947737296840070086399613316";
String sNum = "3.1";
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);
}
}
}
source
share