1>>> for n in range(2, 10):
2... for x in range(2, n):
3... if n % x == 0:
4... print(n, 'equals', x, '*', n//x)
5... break
6... else:
7... # loop fell through without finding a factor
8... print(n, 'is a prime number')
9...
102 is a prime number
113 is a prime number
124 equals 2 * 2
135 is a prime number
146 equals 2 * 3
157 is a prime number
168 equals 2 * 4
179 equals 3 * 3
18
1public List<Integer> factorsOf(int n) {
2 ArrayList<Integer> factors = new ArrayList<>();
3
4 for (int d = 2; n > 1; d++)
5 for (; n % d == 0; n /= d)
6 factors.add(d);
7
8 return factors;
9}
10