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的所有数字整除的最小正数是多少?
思路
1.辗转相除法:
欧几里德算法:假设a,b != 0;gcd(a, b) = gcd(b, a % b);
2.能被a和b整除的最小整数是 a * b / gcd(a, b);
Eg:能被3和5整除的最小整数是15 = 3 * 5 / 1;
#include <stdio.h>
int gcd(int a, int b){
if(!b) return a;
return gcd(b, a % b);
}
int main(){
int ans = 1;
for(int i = 1; i < 20; i++){
ans *= i / gcd(ans, i);
}
printf("%d\n", ans);
return 0;
}