/* Smallest multiple */
/*
* 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?
*/
public class SmallestMultiple {
public static int gcd(int m, int n){
if(n>m){
int temp = m;
m = n;
n = temp;
}
int gcdNum = m;
while(m != 0){
m = gcdNum%n;
gcdNum = n;
n = m;
}
return gcdNum;
}
private static int lcm(int m, int n){
return m*n/gcd(m,n);
}
static int multiple(int start, int end){
if(start > end){
int temp = end;
end = start;
start = temp;
}
else if(start == end)
return start;
else
return lcm(end, multiple(start, end-1));
return multiple(start, end);
}
public static void main(String[] args) {
System.out.println(multiple(11,19)); // line a--Which is equal to multiple(1,19)
System.out.println(multiple(11,20)); // line b--Which is equal to multiple(1,20)
}
}
我认为multiple(1,19) 和multiple(1,20) 的答案会得到相同的结果。但是使用我的代码,它们是不同的,multiple(1,19)=232792560(正确答案)和multiple(1,20)=18044195这是错误答案!!!我知道有很多更简单的算法,但我只想知道我的问题在哪里......谁能告诉我问题?