Java 代码中的错误。能被1到20的所有数字整除的最小正数是多少?

Java 代码中的错误。能被1到20的所有数字整除的最小正数是多少?,java,Java,你好,我正在做一个Euler项目: 2520是最小的数字,可以被1到10之间的每一个数字除,没有任何余数 能被1到20的所有数整除的最小正数是多少 但是我看不出我的代码中有什么错误。 请帮忙 public long mainNumber() { long number=1; for(int i=2;i<=20;) { while(number%i!=0) { number++;

你好,我正在做一个Euler项目:

2520是最小的数字,可以被1到10之间的每一个数字除,没有任何余数

能被1到20的所有数整除的最小正数是多少

但是我看不出我的代码中有什么错误。
请帮忙

public long mainNumber()
{
    long number=1;
    
    
    for(int i=2;i<=20;)
    {
        while(number%i!=0)
        {
            number++;
        }
    i++;    
    }
    return  number;
}

public static void main(String[] args)
{
    Smallest_multiple result =new Smallest_multiple();
     System.out.println("The smalest multiple "+result.mainNumber());
}
public long mainNumber()
{
长数=1;
对于(int i=2;i尝试:

public long mainNumber(){
长数=1;

对于(int i=1;i而言,问题在于您的外部循环可能会完全耗尽,并且您可能仍然找不到您要查找的
编号

你应该这样做:

private static long gcd(long a, long b)
{
    while (b > 0)
    {
        long temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

public static long mainNumber()
{
    long number = 1;
    for(int i = 2; i <= 20; i++) {
        number = number * (i / gcd(number, i));
    }
    return number;
}

public static void main(String[] args)
{
    Smallest_multiple result =new Smallest_multiple();
    System.out.println("The Smallest Multiple: " + result.mainNumber());
}
public long mainNumber()
{
    long number = 3;
    while(true)
    {
        boolean flag = true;
        for(int i = 2; i <= 10; i++) {
            if(number % i != 0) {
                flag = false;
                break;
            }
        }
        if(flag) break;
        else number++;
    }
    return number;
}

public static void main(String[] args)
{
    Smallest_multiple result =new Smallest_multiple();
    System.out.println("The Smallest Multiple: " + result.mainNumber());
}
您当前的算法将因大数而失败,但如果您想尝试一下,则可以执行以下操作:

private static long gcd(long a, long b)
{
    while (b > 0)
    {
        long temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

public static long mainNumber()
{
    long number = 1;
    for(int i = 2; i <= 20; i++) {
        number = number * (i / gcd(number, i));
    }
    return number;
}

public static void main(String[] args)
{
    Smallest_multiple result =new Smallest_multiple();
    System.out.println("The Smallest Multiple: " + result.mainNumber());
}
public long mainNumber()
{
    long number = 3;
    while(true)
    {
        boolean flag = true;
        for(int i = 2; i <= 10; i++) {
            if(number % i != 0) {
                flag = false;
                break;
            }
        }
        if(flag) break;
        else number++;
    }
    return number;
}

public static void main(String[] args)
{
    Smallest_multiple result =new Smallest_multiple();
    System.out.println("The Smallest Multiple: " + result.mainNumber());
}

警告:您需要仔细选择数据类型,因为它可能会溢出较大的数字。

您的数字几乎总是可以被i整除,在数字增加一次后,即当您增加数字时,它会被当前的i值整除,因此它将仅在20处停止,因为这是您要计算的最后一个数字它可以被整除。它需要同时被所有的数整除。这就是你在这里出错的地方。你只需要不断增加这个数,并检查它是否可以被1到20之间的所有数整除。一旦它被整除,那么你就得到了你想要的数。但是在不断增加的时候要注意变量类型的限制恩廷

这真的不是一个困难的算法。这里是它的一个非常简单的实现

public class Test {
    public static void main(String[] args) {
        long number = 0;
        int factors = Integer.parseInt(args[0]);
        boolean found = false;
        while (!found) {
            number++; // OR you could use [number += 2;] instead, as we know it will be even. Performance improvement! :)
            found = true;
            for (int i = 2; i <= factors; i++) {
                found = found && number % i == 0;
            }
        }

        System.out.println("Number: " + number);
    }
}
公共类测试{
公共静态void main(字符串[]args){
长数=0;
int factors=Integer.parseInt(args[0]);
布尔值=false;
而(!found){
number++;//或者您可以使用[number+=2;],因为我们知道这将是偶数。性能改进!:)
发现=真;

对于(int i=2;i为什么你认为你的代码是错误的?你有没有面临任何具体的问题?你有没有得到任何错误/异常/不正确的结果?你的问题是什么?一步一步地尝试,可以被1到3除的最小数字是什么?在顶部当然是在顶部是类公共类最小的\u multipleProjectEuler==SelfAchievment@Jasuri您知道如何使用调试器吗?您可以将该标志用作while条件的一部分,而不是写入while(true)需要休息一下。谢谢大家。我真的很感谢大家的评论。我已经用一个非常简单的正确算法为你们修改了我的答案。其他一些解决方案似乎不必要地复杂。你们也可以通过将数字增加2而不是1来升级这个解决方案,因为我们知道,它甚至必须被赋予第一个阶乘是2,这将提高性能。
public class Test {
    public static void main(String[] args) {
        long number = 0;
        int factors = Integer.parseInt(args[0]);
        boolean found = false;
        while (!found) {
            number++; // OR you could use [number += 2;] instead, as we know it will be even. Performance improvement! :)
            found = true;
            for (int i = 2; i <= factors; i++) {
                found = found && number % i == 0;
            }
        }

        System.out.println("Number: " + number);
    }
}