Q:
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
A:
''' method 1
最小公倍数一定是最大的数的倍数
'''
p = 20
flag = True
while flag:
for i in range(1,21):
if p % i:
p += 20
break
elif i == 20:
flag = False
print p
''' method 2
两两找最小公倍数,a与b的最小公倍数一定是a*[1~b],这里取了i从2520开始,是因为直到20以内不可能有,通用的还是从1开始
so fast
'''
i = 2520
for k in range(1,21):
if i % k > 0:
for j in range(1,21):
if (i*j)%k == 0:
i *= j
break
print i
'''
method 3
两两找最小公倍数,最小公倍数是由a*b/最大公约数得到的,gcd()是取最大公约数的方法
'''
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
def lcm(a, b): return a*b/gcd(a, b)
print reduce(lcm, range(1, 20+1))
分享到:
相关推荐
2. Write a program that reads ten numbers supplied by the user into a 2 by 5 array, and then prints out the maximum and minimum values held in: (a) each row (2 rows) (b) the whole array 3. Use a ...
For a positive integer n, let’s denote function f(n,m) as the m-th smallest integer x that x>n and gcd(x,n)=1. For example, f(5,1)=6 and f(5,5)=11. You are given the value of m and (f(n,m)?n)⊕n,...
2. Write a program that reads ten numbers supplied by the user into a 2 by 5 array, and then prints out the maximum and minimum values held in: (a) each row (2 rows) (b) the whole array 3.Use a...
Find the company that has the smallest payroll. Q3. Consider the following database, where the primary keys are underlined, construct the following SQL queries for this relational database. member...
The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...
2) Write a program which accepts an input k from the keyboard, and which prints out the smallest fibonacci number that is at least as large as k. The program should also print out its position in the ...
Prime number program in c: c program for prime number, this code prints prime numbers using c programming language.... Remember two is the only even and also the smallest prime number
378.Kth_Smallest_Element_in_a_Sorted_Matrix有序矩阵中第K小的元素【LeetCode单
At time t1 = L/R1, the sending host completes transmission and the entire packet is received at the router (no propagation delay). Because the router has the entire packet at time t1, it can begin to...
在这个名为"the-smallest-tree.rar_The Tree"的压缩包中,包含了关于最小生成树算法的源代码,采用的是Visual C++ 6.0编程环境。 在图论中,一个无向图的最小生成树是指包含图中所有顶点的一个子图,且该子图是树形...
标题 "the kth smallest elem" 暗示我们要讨论的是如何在数组或集合中找到第k小的元素,这是一个常见的算法问题,在计算机科学和数据结构领域具有重要意义。描述中提到的 "Clion" 是一个流行的C/C++集成开发环境,...
373._Find_K_Pairs_with_Smallest_Sums查找和最小的k对数字【LeetCode单题讲解系列】
Even the smallest change to service availability can severely impact customer satisfaction, which can cause the enterprise to incur huge losses. Performance tuning is a challenging topic that focuses...
the number of selected items changed while the method was running. - ADD: Added the method TFlexControl.DoNeedHint - allows editing the displaying of hint for flex-object within the object. - ADD: ...
theory describes all motion with the quantum of action ħ, the smallest change observed in nature. Building on this basic idea, the text first shows how to describe life, death and pleasure. Then, the...
C++ LeetCode题解之744. 寻找比目标字母大的最小字母 知识点一:C++编程语言基础 C++是一种静态类型、编译式、通用的编程语言,支持过程化编程、面向对象编程和泛型编程。在解决LeetCode上的算法问题时,C++以其...
在处理多层嵌套循环生成的乘法表时,寻找第k小的数是一项具有挑战性的任务,需要高效的算法来优化查找性能。乘法表通常是一个矩阵,其行和列代表两个自然数序列的乘积,例如,第一行是1乘以1到n,第二行是2乘以1到n...
在解决LeetCode上的668题时,涉及到对乘法表中第k小的数进行寻找。乘法表是由连续的正整数所构成,其中第i行和第j列的数是i*j。题目要求我们找出这样的一个数,它是这个乘法表中按升序排列时的第k个数。...
1 2 5 2 3 3 1 3 6 3 4 2 2 4 4 ``` 读取这些数据后,可以按照Kruskal's Algorithm的步骤进行处理,找出最小树形图。 需要注意的是,对于有向图,可能存在多个解,因为最小树形图不一定唯一。此外,如果图中存在负...
However Intel 8 will produce the fastest and smallest Windows executable overall, followed by Microsoft VC++ .net 7.1 /O2 /G7 PAQ6 DESCRIPTION 1. OVERVIEW A PAQ6 archive has a header, listing the...