[Java] 纯文本查看 复制代码 import java.util.ArrayList;
public class Demo_test00 {
public static void main(String[] args) {
foo(90L);
}
private static void foo(Long num) {
if (num == 0 || num == 1) {
System.out.println(num + "没有质因数");
return;
}
long original = num;
int count = 0;
ArrayList<String> arrayList = new ArrayList<String>();
for (long i = 2; i <= num; i++) {
if (num % i == 0) {
arrayList.add(Long.toString(i));
count++;
num /= i;
i--;
}
}
if (count < 2) {
System.out.println(original + "为质数");
} else {
System.out.print(original + " = " + arrayList.get(0));
for (int i = 1; i < count; i++) {
System.out.print(" * " + arrayList.get(i));
}
System.out.printf("\n%d共有%d个质因数\n", original, count);
}
}
} |