python判断数字在不在范围内_我的程序在python中完成数字并且不确定我是否应该在范围内使用(1,1000)或(2,n 1)...

我在

python上做了完美数字的实验室它运行良好并打印了我需要的数字.但不确定我是否需要将(1,1000)放入范围或(2,n 1)是否正常?我的指示要求我

“编写一个python程序,找到从1到10,000的所有完美数字.当找到一个完美的数字时,你的逻辑应该打印它.”

什么是完全数:

In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself) i.e. σ1(n) = 2n.

当我运行我的程序时,它打印出6,28,496和8128.

n = 1

while True:

factors = [1]

[factors.append(i) for i in range(2,n+1) if n%i == 0]

if sum(factors) == 2*n: print n

n += 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值