I have the following code for prime factorization.
public static void primeFactors(int n) { for (int i = 2; i <= Math.sqrt(n); i = i+1) { while (n%i == 0) { factors.add(i); n = n/i; } } if (n>2 ) { factors.add(n); } System.out.println(factors); } From a mathematical point of view we have to check if each divisor i is also prime and not only if it is factor. Could anyone explain me (mathematically) why the algorithm still works?