![](/img/trans.png)
[英]Write a function that input a positive integer n and return the number of n-digit positive integers that divisible by 17
[英]Accept a positive integer n as input and find the print the smallest integer that is divisible by all the integers in the range [1, n]
我该如何解决这个问题? 我直观地理解,我需要找到 [1,n] 之间数字的 LCM,但如果不使用数学库和 function 在 Python 中定义进程,我无法弄清楚如何编码。 我确实为 C++ 找到了一些解决方案,但它们超出了我的理解。
您需要将不是其他数字除数的数字相乘
一个简单的(非最优)算法是从数字列表中下降并检查是否有任何较小的数字是除数,如果是则将其添加到黑名单中。
然后将所有未列入黑名单的数字相乘:
N = 10
number = 1
skip = set()
for i in range(N, 0, -1):
for j in range(i-1, 1, -1):
if j in skip:
continue
if not i%j:
print(f'{j} divides {i}, skipping {j}')
skip.add(j)
if i not in skip:
number *= i
N=10 时 output:
5 divides 10, skipping 5
2 divides 10, skipping 2
3 divides 9, skipping 3
4 divides 8, skipping 4
30240
那么没有任何优化的最简单的方法是这样的:
def int_div(n):
outp = 0
while(True):
outp += 1
res_list = []
for i in range(1,n+1): res_list.append(outp%i)
if sum(res_list) == 0:
break
return outp
但它仅适用于 <=16 的小数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.