python找出3或者5的倍数求和_3和5的整数倍数求和溢出

我相信这个问题已经被问了很多,但我已经查过其他论坛,并试图解决这个问题,这似乎没有帮助。我在想有一个溢出问题,但我不记得该怎么解决它了。我花了很长一段时间来编写代码(我的错在这里),所以我正在尝试一些问题来帮助我重新回到正常的状态。所以,只是想知道到底出了什么问题。当我尝试n = 1000时,答案是错误的,但是小于这个数字的数字似乎是正确的。因为大数字行不通,我认为这是一个整数溢出。在def n_number():

n = raw_input("Enter a max number: ")

try:

int(n)

return n

except ValueError:

print 'Value is not an integer'

exit(1)

# 'function that will add multiples of 3 and 5 that are less than the given value, n.'

def sum_multiplies(n):

sum = long(0)

counter3, counter5 = int(1),int(1)

value3 = 3*counter3

value5 = 5*counter5

while True:

# 'sums of multiples of 5\'s less than n'

if value5

sum+= value5

counter5+=1

value5 = 5*counter5

# 'sums of multiples of 3\'s less than n'

if value3

sum+= value3

counter3+=1

value3 = 3*counter3

else:

break

print "sum: %s" %sum

print "counter3: %s" %counter3

print "counter5: %s" %counter5

def main():

'max number is in n'

n = n_number()

sum_multiplies(n)

if __name__ == "__main__":

main()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值