Reducing Iterations During Math Factoring
I need assistance with refactoring the inner loop. I would like a more
efficient way including less iterations.
public static void main(String[] args) {
for (int i = 2; i <= 100; i++) {
System.out.print("Factors of " + i + ": ");
for (int j = 2; j < i; j++)
if ((i % j) == 0)
System.out.print(j + " ");
System.out.println();
}
}
Here is what I have attempted thus far:
int j = 2;
do{
if ((i % j) == 0) System.out.print(j + " ");
System.out.println();
j++;
} while (j < 2);
This only prints out the first factor and I cannot for the life of me
figure out how to get it to through the whole list. I know I am missing
something semantically but it is making me pull my hair out.
No comments:
Post a Comment