简体   繁体   English

使用ArrayList的素数生成器出现问题

[英]Problems with prime number generator, using ArrayList

I have been having a heck of a time trying to make a prime number generator work. 我一直在尝试使质数生成器正常工作。 It's supposed to list the first 100 primes, so sieving means either cheating and looking up an artificial limit to the composites, or creating a bunch of needless arrays of composite numbers. 它应该列出前100个素数,所以过筛意味着要么欺骗并查找合成的人为限制,要么创建一堆不必要的合成数字数组。

Just by dividing and testing, I've tried using a simple array of primes and then testing against them, but that left the array mostly empty and returning "x/0" errors. 仅通过划分和测试,我就尝试使用一个简单的质数数组,然后对它们进行测试,但这使该数组大部分为空,并返回“ x / 0”错误。 ArrayList lets me expand the array as needed, but I've had trouble getting my test loop to work. ArrayList使我可以根据需要扩展数组,但是我无法使我的测试循环正常工作。 It's to the point where the variables just look like gibberish in my head and I'm changing values at random. 到了这样的地步,我脑海中的变量就像乱七八糟的东西,我随机改变值。 Can anyone point out where my concept went wrong? 谁能指出我的概念出了什么问题?

public static void main(String[] args) {
    ArrayList<Integer> primes = new ArrayList<>();
    int testNum = 1;

    while (primes.size() < 100)
    {
        for (int index = 0; index <= testNum; index++)
        {
            if ((testNum % primes.get(index)) == 0)
                primes.add(testNum);
        }
        testNum++;
    }

    System.out.println("The first 100 prime numbers are:");
    for (int index = 0; index < 100; index++)
        System.out.println((index + 1) + ": " + primes.get(index));
}

} }

fixed: thanks to Saleem Khan. 固定:感谢萨利姆·汗。 had to mess with the upper limit and a few other things to get it working and printing right. 必须弄乱上限和其他一些事项才能使其正常工作和正确打印。

ArrayList<Integer> primes = new ArrayList<Integer>();
    int testNum;
    int index;

    for (index = 1; index <= 1000; index++) //index counts up to 100 primes
    {
        int factors = 0; 
        for (testNum = index; testNum >= 1; testNum--)
        {
            if (index % testNum == 0)
            {
                factors = factors + 1;
            }
        }

        if (factors == 2)
        {
            primes.add(index);
        }
    }

    System.out.println("The first 100 prime numbers are:");
    for (int prime = 0; prime < 100; prime++)
    {
        System.out.print((prime + 1) + ": ");
        System.out.println(primes.get(prime));
    }
}

edit: deleted erroneous code at 20 and 21 编辑:在20和21删除了错误的代码

Hi use this code and problem is with your logic. 嗨,使用此代码,问题出在您的逻辑上。

public static void main(String[] args) {
    int i = 0;
    int num = 0;

    List<Integer> primes = new ArrayList<Integer>();

    for (i = 1; i <= 100; i++) {
        int counter = 0;

        for (num = i; num >= 1; num--) {
            if (i % num == 0) {
                counter = counter + 1;
            }
        }

        if (counter == 2) {
            primes.add(i);
        }
    }
    System.out.println("The first 100 prime numbers are:");
    for(int prime : primes) {
        System.out.println(prime);
    }
}

you cant get value from empty List, it will throw indexOutOfBoundException 您不能从空列表中获取值,它将抛出indexOutOfBoundException

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM