我确信这个问题已经被问了很多,但我检查了其他论坛并尝试解决这个问题,这似乎没有帮助。我在想有一个溢出问题,但我不记得如何解决它。我从编码中休息了很长时间(我的错在那里),所以我正在尝试一些问题来帮助我重新开始工作。所以,只是想知道出了什么问题。当我尝试时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<int(n):
sum+= value5
counter5+=1
value5 = 5*counter5
# 'sums of multiples of 3\'s less than n'
if value3<int(n):
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()