第五章第三十三题(完全数)(Perfect number)

第五章第三十三题(完全数)(Perfect number)

  • **5.33(完全数)如果一个正整数等于除它本身之外其他所有除数之和,就称之为完全数。例如:6是第一个完全数,因为6 = 1 + 2 + 3.下一个完全数是28 = 14 + 7 + 4 + 2 + 1。10000以下的完全数有四个。编写程序,找出这四个完全数。
    **5.23(Perfect number) A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is the first perfect number because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1. There are four perfect numbers 6 10,000. Write a program to find all these four numbers.
  • 参考代码:
package chapter05;

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

  • 结果显示:
6 28 496 8128 
Process finished with exit code 0

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值