原题如下:
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中每个数整除的正整数是多少?
解题思路:
首先在这20个数中前10位都可以在后10位中找到自己的倍数,也就是说我们要求的最小正整数需要被11.....20整除而这些数中11,13,17,19都是质数,剩下12,14,,16,15,18,20这6个数,我们只需要找出这6个数的最小公倍数再乘以前面的4个质数就可以,可是我找不到而且计算起来相当麻烦......这说明思路是错的。好吧,换一种思路想想
2520是怎么来的呢, 先找出1到10里的质数分别为2,3,5,7,剩下的6,8,9,10,其中6和10由质数之间相乘可以得到,8=2^3,9=3^2,所以2520=2^3*3^2*5^1*7^1,由此可见解决此题的关键就是找到这几个质数的幂,不妨用log函数求得,值得一提的是,由于5,7,11...19,等质数一眼可以看出幂最多为1,所以在代码中没有体现,要自己理解、代码如下:
public class Launcher {
public static void main(String[] args) {
int sum=1;
int a=qiu_mi(2);
int b=qiu_mi(3);
for(int i =1;i<21;i++){
if(is_prime(i)){
sum=sum*i;}
}
sum=(int) (sum*Math.pow(2,a-1)*Math.pow(3,b-1));
System.out.println(sum);
}
public static int qiu_mi(int i){
int a=(int) (Math.log(20)/Math.log(i));
return a;
}
public static boolean is_prime(int i)
{
for(int j=2;j<i;j++){
if(i%j==0)
{
return false;
}
}
return true;
}
}