[Project Euler] 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,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)