- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题陈述
This problem is a programming version of Problem 1 from projecteuler.net
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 N.
输入格式
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer, N.
输出格式
For each test case, print an integer that denotes the sum of all the multiples of 3 or 5 below N.
约束
1≤T≤105
1≤N≤109
示例输入
2
10
100
示例输出
23
2318
我正在做第一个 Project Euler 问题,但有时间限制以应对额外的挑战。如果该过程花费的时间超过 10 秒,它将自动失败。
这是一个示例输入:
2 # number of test cases
10 # first test case
100 # second test case
这是我的代码:
test_case = int(input())
for x in range(0, test_case): # Loops after every test case
stop_value = int(input())
answer = 0
threes = 0
while threes < stop_value: # Checks 3s
answer += threes
threes += 3
fives = 0
while fives < stop_value: # Checks 5s
answer += fives
fives += 5
commons = 0
while commons < stop_value: # Check 15s
answer -= commons
commons += 15
print(answer)
问题在对我的解决方案进行评分时不会向我显示输入,但我假设其中一个测试用例正在检查直到 10^9
,这将花费比运行 10 秒。
上一次尝试注意:最初我有一个更简单的代码,它运行一个从 0
到 stop_value
的 for 循环,一旦 stop_value
变得太大了,所以我试图让 while 循环(我在上面展示过)在所有内容之间跳过。
下一次尝试:
我试图通过它们自己的阶乘找到每个数字的最大倍数和该术语的倍数,但我得到了错误的输出。
为了解释我的思考过程,我以 10 为例,10//3 = 3。如果我做 3!*3,它将是 [1*3,2*3,3*3 ]
= [3,6,9]
是 stop_value
的所有 3 的倍数 编辑:我注意到这个实现不正确,我目前正在考虑阶乘的 for 循环。
import math
test_case = int(input())
for x in range(0, test_case): # Loops after every test case
stop_value = int(input())
threes = stop_value // 3
fives = stop_value // 5
commons = stop_value // 15
answer = math.factorial(threes)*3 + math.factorial(fives)*5 - math.factorial(commons)*15
print(answer)
您的输出(标准输出)
13
26049952856435659498719093244723189200
预期输出
23
2318
最佳答案
这是自然数之和的推广。步长 k
和最大数 n
(n
可被 k
整除)的一般公式为: n/k/2 * (n + k)
。
def euler1 (n):
max3 = range(0, n, 3)[-1]
max5 = range(0, n, 5)[-1]
max15 = range(0, n, 15)[-1]
sum3 = (max3 + 3) * max3 // 3 // 2
sum5 = (max5 + 5) * max5 // 5 // 2
sum15 = (max15 + 15) * max15 // 15 // 2
return sum3 + sum5 - sum15
>>> euler1(10)
23
>>> euler1(100)
2318
>>> euler1(10**100)
23333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333331666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666668
关于python - 我的代码效率低下,来自 Project Euler 的 3 和 5 的倍数,但超时条件为 10 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31412252/
所以这是我的情况。我有几个数字,我想四舍五入到最接近和最小的 10 的倍数。 例如,介于51到59之间的值应四舍五入到50。 Input = 59 = >Respose = 50 Input = 51
我尝试在 select2 多重选择中实现以下场景。 用户选择一些选项 用户在选择中选择“无必需程序” Select 会清除所有选定的值 Select 有一个占位符,其中包含以下文本:“无必需功能” 这
我正在寻找一种方法将数字四舍五入为最接近的 250 的倍数。例如,如果我有以下 JS: var containerHeight = $("#container").height(); ...我们假设“
大家好,我是 AngularJS 的新手,我在调用多个 http.get 时遇到问题。 $scope.countries 正在从城市获取值(value)。发生了什么事?如何调用多个http.get?
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: What does the ** operator do in Python? 以下 python 代码中的
我想用 scss 做点什么。我基本上想要它,所以我可以为每 5px 留出任何余量。因此,例如我可以写 m10 m15 m20 m25 等......它会创建 margin:10px;边距:15px;等
我正在用 C Sharp 创建一架钢琴,目前我有键盘键来播放声音。例如,键 A 播放音符 C。我遇到的问题是我想同时按下多个键并发出声音。显然我不想将所有组合都放在 keyDown 类中,因为我将不得
我仍在寻找Rails优雅的多文件上传方式。 我刚刚了解了“输入类型=”文件”多个” Rails支持吗?有什么例子吗?在Rails中如何实现将多张照片上传到相册模板的技巧? 谢谢 最佳答案 您需要的是更
我有这样的代码,可以创建多个 D3 donut 倍数。 body { font: 10px sans-serif; } svg { padding: 10px 0 0 10px; } .
如何在 numpy 中将数字取整到最接近 0.2 的倍数? 例如,我有这个: 0.2 * np.floor(xi / 0.2) 它在大多数情况下都有效,例如 >>> xi = 9.4 >>> 0.2
这个问题在这里已经有了答案: Rounding numbers to specific multiples [duplicate] (1 个回答) 关闭 6 年前。 我有一个花车。我想将它舍入到最接
我意识到这不是一个很好的标题,所以我会尝试更彻底地解释。 基本上,我有一个只有少量变化的 double 值,称为 clusterSize。然后,我有第二个 double 值,即 map 上的纬度或经度
我在 Canvas 上绘制了一个网格,当用户点击网格时,我正在绘制一个矩形。我想始终在用户单击的网格单元格顶部绘制矩形。所以我需要向下舍入到最接近的 X,在我的例子中,是 40 的倍数。 一些例子..
如何舍入为某些基本浮点单位的偶数倍(例如 0.0005f) float example_input = 2.718281828459f; float unit = 0.0005f; 使得输出的形式为2
我是 Java 编程的新手。我想将价格四舍五入到最接近的小数点后两位。 例如 38.82 变成 38.80 38.87 变成 38.90 38.85 保持不变。我做了例如1 和例如2 但结果只有小数点
我在 float 中给出了值和步数,例如: double step = 0.4; double value = 47.7121; 对于我给出的每个值和步骤对,我需要返回步骤的最接近的可表示 float
我正在尝试在我的一台服务器上运行 Drupal 8,但是在浏览器中安装 Drupal 期间,它为我提供了与运行 php -v 时不同的 PHP 版本 (7.0.23) > 在命令行 (7.1.12)
我需要将数字四舍五入到最接近的 0.11 倍数或四舍五入整数 示例: 0.99 turns to 1.00 0.87 turns to 0.88 0.59 turns to 0.55 54.01 tu
我有一个从公式计算的 double 列表。例如,其中一个 double 是 88.32547。我想将它们转换为最接近的 10 的整数倍,并将它们放入另一个变量中。 在示例中,double a = 88
我正在尝试合并 this multiples example在 this example 中看到焦点鼠标悬停 + 十字准线功能.我预期的 y 大小的十字准线一直在 x 线上射击。 See workin
我是一名优秀的程序员,十分优秀!