我正在研究欧拉项目 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?
但是,我不断得到的答案 116396280 只是实际答案的一半。我哪里错了?为什么我只得到一半的答案?顺便说一下,这是用java编写的。
public class main{
public static void main(String[] args){
long number=2520;//smallest number divisible by all numbers from 1-10
long x;//for loop counter
for (x=19;x>10;x-=2){
if (x!=15){//its factors 5 and 3 have already been counted
number*=x;//multiplies new prime numbers
}
}
System.out.println(number);
}
}
最佳答案
如果只遍历奇数,您会错过 16,其因式分解为 24 您的答案仅包含 23。
关于java - Project Euler 5,我得到的答案只有一半,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35402257/