JAVA经典算法(九)

题目:一个数如果恰好等于它的因子之和,这个数就称为 "完数 "。例如6=1+2+3.编程   找出1000以内的所有完数。

package cn.ls.lanqiao;

public class Test9 {
	public static void main(String[] args) {
		int ls;
		for (int i = 1; i <= 1000; i++) {
			ls = 0;
			for (int j = 1; j < i; j++) {
				if (i % j == 0) {
					ls += j;
				}
			}
			if (ls == i) {
				System.out.print(i + " ");
			}

		}

	}
}

 结果:6 28 496

发布了151 篇原创文章 · 获赞 164 · 访问量 9803

猜你喜欢

转载自blog.csdn.net/ls_wifi/article/details/103941365
今日推荐