不知道我是否应该已经张贴了这个对math.stackexchange代替,但它包含更多的节目,所以我把它贴在这里。
这个问题似乎很简单,但我已经在这里坐了至少一个小时,现在还没有计算出来。我已经尝试了不同的解决方案,并阅读数学公式,它等,但编码时,它不会给我正确的答案!我做了两个不同的解决方案是,两者都让我有错误的答案。第一个解决方案给我265334,而第二个给我232169.答案是233168,那么第二个解决方案接近。
我应该提到这是来自项目欧拉,第一个为precise。
下面是我的code。任何想法有什么不对?
NUMS = [3,5] 最大= 999 结果= 0 对于NUM在NUMS: 因为我在范围内(1,最大值): 如果num * I<最大: 结果+ = NUM *我 打印结果 结果= 0 对于i在范围(0,最大值): 如果我%3 == 0或I%5 == 0: 结果+ = I 打印结果解决方案
系列(K,最大)不包括最高,所以你真的检查直至并包括998(而999是3的倍数)。使用范围(1,1000)代替。
Not sure if i should've posted this on math.stackexchange instead, but it includes more programming so i posted it here.
The question seems really simple, but i've sat here for at least one hour now not figuring it out. I've tried different solutions, and read math formulas for it etc but it wont give me the right answer when coding it! I made two different solutions for it, which both gives me the wrong answer. The first solution gives me 265334 while the second one gives me 232169. The answer is 233168, so the second solution is closer.
I should mention this is a question from Project Euler, the first one to be precise.
Here's my code. Any ideas what's wrong?
nums = [3, 5] max = 999 result = 0 for num in nums: for i in range(1,max): if num*i < max: result += num*i print result result = 0 for i in range(0,max): if i%3 == 0 or i%5 == 0: result += i print result解决方案
range(k,max) does not include max, so you're really checking up to and including 998 (while 999 is a multiple of 3). Use range(1,1000) instead.
更多推荐
Python的:找到3所有倍数低于1000的总和或5
发布评论