the smallest positive number

简介: 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?   程序比较好编,就是考验计算机。

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?

 

程序比较好编,就是考验计算机。最大范围也是预估的,边写边调。

尽管算对了,但方式可能不对。

def div2(n,x):
    isDiv = True
    for i in xrange(2,n):
        if x % i != 0 :
            isDiv = False
            break
    return isDiv
for i in xrange(2,1772146456):
    if div2(21,i) == True:
        print i
        break

输出:

C:\webpy\webpy\Scripts\python.exe C:/pycode/euler.py
232792560

Process finished with exit code 0

后来请高手重新写了一个,这个就很正规了。

def getSmallestNum(m,n):
    for j in range(1,n):
        if( m*j%n ==0 ):
            return m*j
    return m*n

smallestNum = 1
for i in range(2,21):
    if(smallestNum%i !=0):
        smallestNum = getSmallestNum(smallestNum,i)

print smallestNum

 

目录
相关文章
|
API
LeetCode 375. Guess Number Higher or Lower II
我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字。 每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了。 然而,当你猜了数字 x 并且猜错了的时候,你需要支付金额为 x 的现金。直到你猜到我选的数字,你才算赢得了这个游戏。
79 0
LeetCode 375. Guess Number Higher or Lower II
|
API
LeetCode 374. Guess Number Higher or Lower
我们正在玩一个猜数字游戏。 游戏规则如下: 我从 1 到 n 选择一个数字。 你需要猜我选择了哪个数字。 每次你猜错了,我会告诉你这个数字是大了还是小了。
54 0
LeetCode 374. Guess Number Higher or Lower
Maximum Subsequence Sum
最大连续子列和问题,在此给出题解 (浙大PTA https://pintia.cn/problem-sets/16/problems/665)
成功解决ValueError: min_samples_split must be an integer greater than 1 or a float in (0.0, 1.0]; got th
成功解决ValueError: min_samples_split must be an integer greater than 1 or a float in (0.0, 1.0]; got th
1104. Sum of Number Segments (20) consecutive subsequence 每个数出现的次数
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence.
938 0
|
人工智能 机器学习/深度学习
1007. Maximum Subsequence Sum (25)
简析:求最大子列和,并输出其首末元素。在线处理,关键在于求首末元素。 本题囧,16年9月做出来过,现在15分钟只能拿到22分,有一个测试点过不了。
948 0