一个数如果恰好等于它的因子之和,这个数就称为完数
方法1:
package www.wl.java;
public class Test {
public static void main(String[] args) {
for(int i=1;i<1000;i++) {
if (isWanShu(i)){
System.out.println (i);
}
}
}
public static boolean isWanShu(int n) {
int sum = 0;
for (int j = 1; j < n; j++) {
if (n % j == 0) {//只要能被%完为0,及为因子
sum += j;
}
}
if (sum == n) {
return true;
}
return false;
}
}
方法二:
public class 完数 { public static void main(String[] args) { for (int i = 1; i < 1000; i++) { int sum = 0; for (int j = 1; j < i; j++) { if (i % j == 0) {//只要能被%完为0,及为因子 sum += j; } } if (sum == i) { System.out.println (i); } } } }