`
诗意的栖居
  • 浏览: 268095 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

No.5 The smallest positive number

 
阅读更多
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))
分享到:
评论

相关推荐

    B04_2841_EX08.zip_2841_Number Ten

    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 ...

    code_hdu.rar_ACM_The First_hdu_test case example

    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,...

    C语言实验作业

    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...

    SQLAssignment for Chapter 3

    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...

    2020年初升高一衔接班英语期末考试.doc

    一 单项选择(共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

    HDU1019(2028)解题报告

    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...

    matlabmatrix.rar_4 3 2 1_Fibonacci

    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 ...

    qwerty.rar_The One And Only

    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单题讲解系列】

    378.Kth_Smallest_Element_in_a_Sorted_Matrix有序矩阵中第K小的元素【LeetCode单

    Data Structure and Algorithms

    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单题讲解系列】

    373._Find_K_Pairs_with_Smallest_Sums查找和最小的k对数字【LeetCode单题讲解系列】

    the-smallest-tree.rar_The Tree

    这个源代码是一个最小生成树的算法实现,采用的visual c++6.0

    C# Duplicate.docx

    (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.

    the kth smallest elem

    本人在Clion中的实现的第小k元素,有期望值是O(n)代价的以及醉话情况为O(n)BFPRT算法

    Java.EE.7.Performance.Tuning.and.Optimization.178217642X

    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...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    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: ...

    motion mountain-volume 5.part 2

    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...

    kgb档案压缩console版+源码

    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...

    Python Cookbook, 2nd Edition

    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 ...

Global site tag (gtag.js) - Google Analytics