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...
一 单项选择(共15小题,每小题1分) ...5. As the smallest child of his family, Alex is always longing for the time he should be able to be independent. A. which B. where C. whom D. when
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单
3.6 Finding the smallest and largest values in the binary search tree 25 3.7 Tree Traversals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26 3.7.1 Preorder . . . . . . . . . . . . . . . . ....
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...
373._Find_K_Pairs_with_Smallest_Sums查找和最小的k对数字【LeetCode单题讲解系列】
这个源代码是一个最小生成树的算法实现,采用的visual c++6.0
(Duplicate Elimination) Use a one-dimensional array to solve...Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user inputs each new value.
本人在Clion中的实现的第小k元素,有期望值是O(n)代价的以及醉话情况为O(n)BFPRT算法
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...
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...
Calculating the Number of Weekdays Between Two Dates Recipe 3.6. Looking up Holidays Automatically Recipe 3.7. Fuzzy Parsing of Dates Recipe 3.8. Checking Whether Daylight Saving Time Is ...