查找可被 5 或 3 整除的数字的程序
Programs that Find Numbers Divisible by 5 or 3
我想编写一个程序来找出一个数中有多少个数可以被 3 或 5 整除例如 10 有 3 9 6 可以被 3 整除并且有 5 和 10 可以被 5 整除所以总数是 5 等等所以我写我的代码
import math
n=float(raw_input())
div3=(n-2)/3
div5=(n-4)/5
f1=math.ceil(div3)
f2=math.ceil(div5)
sumss=f1+f2
print int(sumss)
但在某些数字中它得到错误的答案并且输入数字的范围将是
从 1 到 10^18,所以我需要在其中使用数学,因为问题测试的时间限制是 2 秒,任何人都有任何有效的方程式来使循环不能让它花费很长时间
这可能是一个项目欧拉问题。问题是某些号码可以由 3
和 5
共享。例如 22
:
3
的约数:3
6
、9
、12
、15
, 18
, 21
.
5
的约数:5
10
、15
、20
因为这两个 15
都发生了,所以你重复计算了。
优点是3
和5
互质,所以共享的数只有能被15
整除的数。所以你只需要撤销重复计算:
n=int(raw_input())
div3=n//3
div5=n//5
div15=n//15
sumss=div3+div5-div15
print sumss
如果你允许重复计算(15
应该被计算两次),你可以简单地使用:
n=int(raw_input())
div3=n//3
div5=n//5
sumss=div3+div5
print sumss
请注意,程序省略了浮点运算:这将导致更快和更精确的程序,因为浮点数使用有限的尾数,因此可能无法正确表示大数(由小错误导致) .此外,一般整数运算速度更快。
欧拉计划 #1
现在 Project Euler 的问题陈述有点不同:它要求求和 这些数字。为此,您必须构造一个表达式来总结 k 的第一个 l 倍数:
k
---
\
/ l*i
---
i=1
使用 Wolfram Alpha,得到 this expression。所以你可以计算这些:
def suml (k,l) :
return k*(k+1)*l/2
n=int(raw_input())
div3=n//3
div5=n//5
div15=n//15
sumss=suml(div3,3)+suml(div5,5)-suml(div15,15)
print sumss
此程序为 n=22
给出 119
,如果您只计算 15
一次,您可以在上面验证它是正确的。
我不确定我是否答对了问题,但这里有一些想法:
n=float(raw_input())
div3=int(n/3)
div5=int(n/5)
div15=int(n/15)
sumss=div3+div5-div15
print sumss
编辑:啊,找到了 Euler 项目。
If we list all the natural numbers below 10 that are multiples of 3 or
5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
这是一个与此处 post 提出的问题不同的任务。它说 波纹管 数字和 求和 。
我不确定post这里的解决方案是否正确,所以我宁愿不这样做。
EDIT2:来自欧拉计划:
We hope that you enjoyed solving this problem. Please do not deprive
others of going through the same process by publishing your solution
outside Project Euler. If you want to share your insights then please
go to thread 1 in the discussion forum.
我想编写一个程序来找出一个数中有多少个数可以被 3 或 5 整除例如 10 有 3 9 6 可以被 3 整除并且有 5 和 10 可以被 5 整除所以总数是 5 等等所以我写我的代码
import math
n=float(raw_input())
div3=(n-2)/3
div5=(n-4)/5
f1=math.ceil(div3)
f2=math.ceil(div5)
sumss=f1+f2
print int(sumss)
但在某些数字中它得到错误的答案并且输入数字的范围将是 从 1 到 10^18,所以我需要在其中使用数学,因为问题测试的时间限制是 2 秒,任何人都有任何有效的方程式来使循环不能让它花费很长时间
这可能是一个项目欧拉问题。问题是某些号码可以由 3
和 5
共享。例如 22
:
3
的约数:3
6
、9
、12
、15
, 18
, 21
.
5
的约数:5
10
、15
、20
因为这两个 15
都发生了,所以你重复计算了。
优点是3
和5
互质,所以共享的数只有能被15
整除的数。所以你只需要撤销重复计算:
n=int(raw_input())
div3=n//3
div5=n//5
div15=n//15
sumss=div3+div5-div15
print sumss
如果你允许重复计算(15
应该被计算两次),你可以简单地使用:
n=int(raw_input())
div3=n//3
div5=n//5
sumss=div3+div5
print sumss
请注意,程序省略了浮点运算:这将导致更快和更精确的程序,因为浮点数使用有限的尾数,因此可能无法正确表示大数(由小错误导致) .此外,一般整数运算速度更快。
欧拉计划 #1
现在 Project Euler 的问题陈述有点不同:它要求求和 这些数字。为此,您必须构造一个表达式来总结 k 的第一个 l 倍数:
k
---
\
/ l*i
---
i=1
使用 Wolfram Alpha,得到 this expression。所以你可以计算这些:
def suml (k,l) :
return k*(k+1)*l/2
n=int(raw_input())
div3=n//3
div5=n//5
div15=n//15
sumss=suml(div3,3)+suml(div5,5)-suml(div15,15)
print sumss
此程序为 n=22
给出 119
,如果您只计算 15
一次,您可以在上面验证它是正确的。
我不确定我是否答对了问题,但这里有一些想法:
n=float(raw_input())
div3=int(n/3)
div5=int(n/5)
div15=int(n/15)
sumss=div3+div5-div15
print sumss
编辑:啊,找到了 Euler 项目。
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
这是一个与此处 post 提出的问题不同的任务。它说 波纹管 数字和 求和 。
我不确定post这里的解决方案是否正确,所以我宁愿不这样做。
EDIT2:来自欧拉计划:
We hope that you enjoyed solving this problem. Please do not deprive others of going through the same process by publishing your solution outside Project Euler. If you want to share your insights then please go to thread 1 in the discussion forum.