java - Project Euler 5,我得到的答案只有一半,为什么?

标签 java

我正在研究欧拉项目 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/

相关文章:

java - 生成 XML 时 JAX-RS 的奇怪响应

java - 如何添加具有点击状态的用户可配置按钮?

java - 从java运行linux命令,错误500,无法运行程序没有那个文件或目录

java - NGN 堆栈主叫方 ID/详细信息问题

java - 带有 SearchView 的 Android abs,onQueryTextListener 不工作

java - 带复选框的 Spring DataGrid

java - 使用 Java API 将多个属性设置为 titan 中的边

java - Struts 1 ActionForm 中的 ArrayList 未填充

java - Canvas/JPanel 未显示在 JFrame 上?

java - 用于解析(字符串)方程的解释器和数据结构