完美数,不明白的去bing搜索吧。
下面给程序代码,没有加多线程,不过现在的CPU已经很快了,瞬间就完了。
package com.yourcompany.struts; public class PerfectNumber { public static void main(String[] args) { // int sum, k; int perfectNum[] = new int[100]; for (int num = 1; num <= 10000; num++) { int sum = 0; int maxPer = 0; for (int i = 1; i <= num - 1; i++) { if (num % i == 0) { sum = sum + i; perfectNum[maxPer++] = i; } } if (num == sum) { System.out.print(num + "="); for (int i = 0; i < maxPer; i++) { if (i > 0) { System.out.print("+"); } System.out.print(perfectNum[i]); } System.out.print("\n"); } } } }
运行结果如下:
6=1+2+3
28=1+2+4+7+14
496=1+2+4+8+16+31+62+124+248
8128=1+2+4+8+16+32+64+127+254+508+1016+2032+4064
时间: 2024-10-26 03:24:42