这个,先用Math.sqrt(1000)求出最大的一个n,然后for(int i = 2; i <= n; i++)输出n*n
用你这种方法好象不行。我没有试出来。如果这个数为6的话就算不出来了。public static void main(String[] args) { int i, j, total; for (i = 2; i < 2000; i++) { total = 0; for (j = 1; j <= i/2; j++) { if (i % j == 0) { total = total + j; } } if (total == i) { System.out.println("完全数有: " + i); } } }
啊,看成完全平方数了,不好意思………………那么一种写法是直接判定 public static boolean isPerfect(int n) { if (n < 0) return isPrime(-n); if (n <= 1) return false; int total = 1; int sqrt = (int) Math.sqrt(n); for (int i = 2; i <= sqrt; i++) if (n % i == 0) total += i + n / i; return total == n; } public static void main(String[] args) throws Exception { int count = 0; System.out.print("有完全数"); for (int i = 0; i <= 1000; i++) { if (isPerfect(i)) { System.out.print(i + ","); count++; } } System.out.print("\n共有"+ count +"个完全数"); }另外可以利用完全数公式:(2^n-1)*2^(n-1),其中n=质数2、3、5、7……………… public static void main(String[] args) throws Exception { int count = 0; System.out.print("有完全数"); for (int i = 0; ; i++) { if (isPrime(i)) { int n = (int) Math.pow(2, i - 1) * ((int) Math.pow(2, i) - 1); if (n <= 1000) { System.out.print(n + ","); count++; } else break; } } System.out.print("\n共有"+ count +"个完全数"); }
第二种写法修改了下,这样比较好,判定可以少一些…… public static void main(String[] args) throws Exception { int count = 0; System.out.print("有完全数"); for (int i = 1;; i++) { int n = (int) Math.pow(2, i - 1) * ((int) Math.pow(2, i) - 1); if (n <= 1000) { if (isPrime(i)) { System.out.print(n + ","); count++; } } else break; } System.out.print("\n共有" + count + "个完全数"); }
额,貌似那个公式中n并不是对所有质数都成立的……………… 如果2^n-1质数,那么(2^n-1)*2^(n-1)便是一个完全数于是再做一下修改…… public static void main(String[] args) throws Exception { int count = 0; System.out.print("有完全数"); for (int i = 1;; i++) { int n1 = (int) Math.pow(2, i - 1); int n2 = ((int) Math.pow(2, i) - 1); int n = n1 * n2; if (n <= 1000) { if (isPrime(i) && isPrime(n2)) { System.out.print(n + ","); count++; } } else break; } System.out.print("\n共有" + count + "个完全数"); }
{
int i, j, total;
for (i = 2; i < 2000; i++)
{
total = 0;
for (j = 1; j <= i/2; j++)
{
if (i % j == 0)
{
total = total + j;
}
}
if (total == i)
{
System.out.println("完全数有: " + i);
}
}
}
public static boolean isPerfect(int n) {
if (n < 0)
return isPrime(-n);
if (n <= 1)
return false;
int total = 1;
int sqrt = (int) Math.sqrt(n);
for (int i = 2; i <= sqrt; i++)
if (n % i == 0)
total += i + n / i;
return total == n;
} public static void main(String[] args) throws Exception {
int count = 0;
System.out.print("有完全数");
for (int i = 0; i <= 1000; i++) {
if (isPerfect(i)) {
System.out.print(i + ",");
count++;
}
}
System.out.print("\n共有"+ count +"个完全数");
}另外可以利用完全数公式:(2^n-1)*2^(n-1),其中n=质数2、3、5、7………………
public static void main(String[] args) throws Exception {
int count = 0;
System.out.print("有完全数");
for (int i = 0; ; i++) {
if (isPrime(i)) {
int n = (int) Math.pow(2, i - 1) * ((int) Math.pow(2, i) - 1);
if (n <= 1000) {
System.out.print(n + ",");
count++;
} else
break;
}
}
System.out.print("\n共有"+ count +"个完全数");
}
public static void main(String[] args) throws Exception {
int count = 0;
System.out.print("有完全数");
for (int i = 1;; i++) {
int n = (int) Math.pow(2, i - 1) * ((int) Math.pow(2, i) - 1);
if (n <= 1000) {
if (isPrime(i)) {
System.out.print(n + ",");
count++;
}
} else
break;
}
System.out.print("\n共有" + count + "个完全数");
}
如果2^n-1质数,那么(2^n-1)*2^(n-1)便是一个完全数于是再做一下修改…… public static void main(String[] args) throws Exception {
int count = 0;
System.out.print("有完全数");
for (int i = 1;; i++) {
int n1 = (int) Math.pow(2, i - 1);
int n2 = ((int) Math.pow(2, i) - 1);
int n = n1 * n2;
if (n <= 1000) {
if (isPrime(i) && isPrime(n2)) {
System.out.print(n + ",");
count++;
}
} else
break;
}
System.out.print("\n共有" + count + "个完全数");
}