Primary Practice h52
将 num 进行质因数分解,将分解到的质因数放到 Set 里面返回
如果每次对 num 分解都从最小的质数开始,那么分解的结果一定都是质数。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | public Set<Integer> decompose(int num) {Set<Integer> result = new HashSet<>();
 if (num < 0) {
 return null;
 } else if (num == 1) {
 result.add(1);
 } else {
 int i = 2;
 while (i <= num) {
 if (num % i == 0) {
 result.add(i);
 num = num / i;
 i = 2;
 } else {
 i++;
 }
 }
 }
 return result;
 }
 
 |