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?

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

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


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

the smallest positive number

时间: 2024-10-11 14:15:59

the smallest positive number的相关文章

LeetCode 41:First Missing Positive Number

Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 思路:用桶排序.重复使用所给的空间. public class Solution { public in

projecteuler Smallest multiple

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是能被从1到10整除的最小整数,求出能被从1到20整除的最小数. =====

PE5 Smallest multiple

题目 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? https://projecteuler.net/problem=5 分析 可以将题

Project Euler: Solution for Problem 5

Smallest multiple Problem 5 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? Source link: http

欧拉计划(python) problem 5

Smallest multiple Problem 5 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? python code : imp

欧拉项目005:最小公倍数

Smallest multiple Problem 5 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? 就是找出1....20 所有数的最

从如何判断浮点数是否等于0说起——浮点数的机器级表示

本文很大程度上收到林锐博士一些文章的启发,lz也是在大学期间读过,感觉收益良多,但是当时林锐也是说了结论,lz也只是知其然,而不知其所以然,为什么要那样写?为什么要这样用?往往一深究起来就稀里糊涂了,现在有幸还是继续读书,我发现了很多问题理解的还不透彻,亡羊补牢. 比如:有int d;  int *d; bool d; double d:几个变量,经过一系列的计算之后,那么去判断这个四个变量是否等于0该怎么做? 很多菜鸟或者编程功底不扎实的就会出错,一些烂书,尤其国内的一部分大学教材,教授编程语

projecteuler---->problem=5----Smallest multiple n个数求最小公倍数

title: 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是能被1到10的自然数整除的最小正整数. 那么,能被1到20

图像的归一化互信息Normlized Mutual Information

紧接上文: 计算二维离散随机变量的联合概率分布 我们知道了上文提到的几种计算二维概率密度分布中, accumarray 方法是最快的. 那么就使用accumarray来求计算两幅相同大小图像的归一化互信息. 互信息的定义 离散变量的互信息定义为: 求联合分布和边缘分布会用到了上文的方法. 或者使用熵来定义: 其中, H是熵. 熵是测量信号或者图像中信息量大小的量. 常用定义式: 归一化互信息定义为: 所以, 不论是求互信息还是求归一化互信息, 都要把两个随机变量的联合分布和边缘分布求出来. 而边