I currently have a number factors program that performs the following things: The user enters a number, checks whether it is a perfect square (yes or no), prints the square root if it is a perfect square, determining the number if it is prime or not, and the number factors of that specific number.
import java.util.Scanner;
import java.util.Date;
import java.text.SimpleDateFormat;
public class NumberFacts {
public static void main(String[] args) {
// Initialize the Scanner
Scanner in = new Scanner(System.in);
//Prompt the user to input the number to evaluate
System.out.print("Enter a number (from 1 to 10000): ");
// Store the input number
int myNum = in.nextInt();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date start = new Date();
startProcess(start);
System.out.println("\nNumber\tPerfect Sqr\tSqrt\tPrime\tFactors");
// For each number iteration, we should begin by bringing a pair each
// time until we hit that number.
iterNum(myNum);
in.close();
Date stopTime = new Date();
long diff = stopTime.getTime() - start.getTime();
stopProcess(dateFormat, start, stopTime, diff);
} // end of main method
private static void iterNum(int n) {
for (int iter = 1; iter <= n; iter++) {
// Start the factorPair as an empty string
String factorPair = "";
// Initialize the loop counter.
int i = 1;
// Result returns the number the user entered.
int result = iter;
// Used to store for pairs.
String myPairs = "";
// Main counter for our pairs.
int pairsCounter = 0;
// Initialize square root variable with 0.
int sqrt = 0;
// Initialize factor variables
int factor1;
int factor2;
// Begin factoring pairs
do {
if (iter % i == 0) {
// Starts with the initial number
// until the last number the user
// specified.
factor1 = i;
// We divide each number by 1 and
// truncate each number to have a
// precise number.
factor2 = iter / i;
// If the number is a factor pair, then
// output the number.
if (factor1 <= factor2) {
factorPair = "(" + factor1 +", "+ factor2 + ")";
pairsCounter++;
myPairs += factorPair;
}
// Checks whether the number is a square root
// number that will assume either yes
// or no.
if (factor1 == factor2) {
sqrt = factor1;
}
}
} while (++i <= result); // End of the factoring pair loops.
// Print the expected formatted output for the program.
printOutput(iter, sqrt, pairsCounter, myPairs);
}
}
private static void startProcess(Date s) {
System.out.println("Started processing at " + s);
}
private static void stopProcess(SimpleDateFormat d, Date n1, Date n2, long l) {
System.out.println("\n\nStart processing " + d.format(n1) + "\n\nStop processing: " + d.format(n2) + "\n\nDifference: " + l + " ms");
}
private static void printOutput(int a, int sq, int c, String s) {
System.out.printf("%n %d \t%s\t\t%s\t%s\t%s", a, sq > 0 ? "Yes" : "No", sq > 0 ? Integer.toString(sq) : "", c > 1 ? "No" : "Yes", s);
}
} // end of class
When compiled and start it, I do the following:
Enter a number (from 1 to 10000): 25
Started processing at Tue Sep 11 22:03:30 CDT 2018
Number Perfect Sqr Sqrt Prime Factors
1 Yes 1 Yes (1, 1)
2 No Yes (1, 2)
3 No Yes (1, 3)
4 Yes 2 No (1, 4)(2, 2)
5 No Yes (1, 5)
6 No No (1, 6)(2, 3)
7 No Yes (1, 7)
8 No No (1, 8)(2, 4)
9 Yes 3 No (1, 9)(3, 3)
10 No No (1, 10)(2, 5)
11 No Yes (1, 11)
12 No No (1, 12)(2, 6)(3, 4)
13 No Yes (1, 13)
14 No No (1, 14)(2, 7)
15 No No (1, 15)(3, 5)
16 Yes 4 No (1, 16)(2, 8)(4, 4)
17 No Yes (1, 17)
18 No No (1, 18)(2, 9)(3, 6)
19 No Yes (1, 19)
20 No No (1, 20)(2, 10)(4, 5)
21 No No (1, 21)(3, 7)
22 No No (1, 22)(2, 11)
23 No Yes (1, 23)
24 No No (1, 24)(2, 12)(3, 8)(4, 6)
25 Yes 5 No (1, 25)(5, 5)
Start processing 2018/09/11 22:03:30
Stop processing: 2018/09/11 22:03:30
Difference: 13 ms
It runs fast with small numbers but when it comes to bigger numbers such as 6000
to 10000
, the speed gets slower with the difference from the start to finish. Inputting 10,000 numbers averages around 800 to 900ms each run.
How can I improve the speed of the computation time in each program run?