[Project Euler] Problem 5
2011-02-22 00:33
302 查看
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,2,3 ··· ··· 20的最小公倍数
我们可惜先求1,2的最小公倍数2
再求2,3的最小公倍数6
再求6,4的最小公倍数12
... ...
而求最小公倍数,我们可以用两数的积除以它们的最大公约数
最大公约数我们用辗转相除法
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
题目的要求就是求1,2,3 ··· ··· 20的最小公倍数
我们可惜先求1,2的最小公倍数2
再求2,3的最小公倍数6
再求6,4的最小公倍数12
... ...
而求最小公倍数,我们可以用两数的积除以它们的最大公约数
最大公约数我们用辗转相除法
#include <iostream> using namespace std; int getLCM(int,int); int getGCD(int,int); int main(){ int multiple = 1; for(int i=1; i<=20; i++){ multiple = getLCM(multiple,i); } cout << multiple << endl; return 0; } int getLCM(int a,int b){ int c = getGCD(a,b); return a/c*b; } int getGCD(int a,int b){ int tmp; while(a%b != 0){ tmp = a; a = b; b = tmp%b; } return b; }
相关文章推荐
- 【Project Euler】【Problem 2】Even Fibonacci numbers
- [Project Euler]加入欧拉 Problem 12
- [Project Euler] Problem 2
- [Project Euler] Problem 19
- Project Euler:Problem 40 Champernowne's constant
- [Project Euler] Problem 29
- [Project Euler]Problem 1:Multiples of 3 and 5
- [Project Euler] Problem 18 & Problem 67 Python解答
- Project Euler:Problem 56 Powerful digit sum
- Project Euler:Problem 80 Square root digital expansion
- Project Euler__problem 6
- Project Euler - Problem 9
- [Project Euler] Problem 56
- [Project Euler]Problem 44
- 【Project Euler】【Problem 3】Largest prime factor
- [Project Euler]加入欧拉 Problem 17
- [Project Euler] Problem 20
- Project Euler: Problem 9 Special Pythagorean triplet
- [Project Euler]Problem 10
- [Project Euler] Problem 30