欧拉项目005:最小公倍数
来源:互联网 发布:c语言中产生随机数 编辑:程序博客网 时间:2024/05/01 14:50
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 所有数的最小公倍数。
参考文章:http://files.cnblogs.com/skyivben/005_overview.pdf。
最简单的就是从1,20两两求最小公倍数。
这里有一个公式:
lcm(1...n) == lcm([n/2]+1,n),[]是向上取整
我自己举了好多例子,在[n/2]+1...n之间的数,总能用(1...[n/2])里的数相乘得到,所以lcm(1...n)==lcm([n/2]+1,n)
说是:对于 1 至 之间的每个数 a,在 至 n 之间刚好可以找到一个数 b,使得 2ka = b 。
我的python代码如下:
#lcm(1...n) == lcm([n/2]+1...n)def gcd(a,b): #a>b while b: r = a % b a = b b = r return adef lcm(a,b): return a/gcd(a,b)*bN = 20x = N/2+1for y in range(N/2+2,N+1): x = lcm(x,y)print x
0 0
- 欧拉项目005:最小公倍数
- 欧拉项目第一题.
- 欧拉项目18题
- 欧拉项目第2题.
- 欧拉项目第3题
- About Project Euler 欧拉项目
- 欧拉项目【ProjectEuler】系列-第一题
- 欧拉项目【ProjectEuler】系列-第二题
- 欧拉项目——Problem21
- 欧拉项目第15题
- 欧拉项目05 ruby版
- 欧拉项目第15题
- 欧拉项目第五题 Smallest multiple
- 最小公倍数
- 最小公倍数
- 最小公倍数
- 最小公倍数
- 最小公倍数
- zip_support/ioapi
- DispatchMessage是异步的吗
- Oracle基础知识(二十) - 临时表
- 算法java实现--分支限界法--0-1背包问题
- test
- 欧拉项目005:最小公倍数
- MFC 添加状态栏
- F# 20分钟快速上手(一)
- java中返回临时对象的问题,求大神破解
- 让金山词霸在 Adobe Reader XI 的 PDF 中取词
- CSS 内阴影
- 分享--linux 学习之路(学linux必看)
- 拼表隐藏第二也表头
- 特信发布超导LTE射频前端系统 大幅改进LTE基站性能