Python的:找到3所有倍数低于1000的总和或5

编程入门 行业动态 更新时间:2024-10-27 00:27:06
本文介绍了Python的:找到3所有倍数低于1000的总和或5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

不知道我是否应该已经张贴了这个对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

本文发布于:2023-11-29 19:22:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1647391.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:倍数   总和   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!