简体   繁体   English

计算素数时没有输出

[英]there is no output on calculating prime number

i want to print first 100 prime numbers. 我想打印前100个素数。 so, i created an array of int 100. I added first prime, bag[0] = 2, then for the following numbers, I tried to write an algorithm. 所以,我创建了一个int 100数组。我添加了第一个prime,bag [0] = 2,然后对于以下数字,我试着编写一个算法。 It will start from 3 and goes on until array is full. 它将从3开始并继续直到阵列已满。 Every number is decided to be if it is prime by whether it is divisible by the previous elements in array and if it is prime then it will be added to array. 如果它是否可以被数组中的前一个元素整除,那么每个数字都被确定为是素数,如果它是素数,那么它将被添加到数组中。

here is my code: 这是我的代码:

public class Trial02 
{   
    public static void main( String[] args) 
    {       
        int[] bag = new int[100];
        bag[0] = 2; //first element of prime array
        int valid = 1;
        int i;
        boolean result = true;
        String str = "";

        //starting from 3 it checks if a number is prime until array is full
        for( i=3; valid<bag.length; i++)
        {
            //it checks if previous primes in array are divisible by current number until coming to current number
            for(int k=0; k<valid; k++)
            {
                if( i % bag[k] == 0)
                    result = false;
            }

            if( result == true) //if it is prime it is added to array
            {
                bag[valid] = i;
                valid ++;
            }
        }

        //printing results
        for(int m=0; m < bag.length; m++)
            str = str + bag[m] + " ";
        System.out.println("zaa xd");
        System.out.println(str);
    }

}

but it don't give any output, just a blank. 但它没有给出任何输出,只是一个空白。 I couldn't find where my mistake is. 我无法找到我的错误所在。 Thanks in advance. 提前致谢。

你从来没有真正检查一个数字是否是素数( result没有设置在任何有用的地方)

It looks like you need to reset result = true; 看起来你需要重置result = true; inside the first for loop. 在第一个for循环中。 Your code as posted sets result = false and then never changes it. 您的代码已发布设置为result = false ,然后永远不会更改它。

The most obvious error is that your boolean result = true; 最明显的错误是你的boolean result = true; is outside the loop: once set to false , it never gets set back to true . 在循环之外:一旦设置为false ,它永远不会被设置回true You do not see any output because your program never stops. 您没有看到任何输出,因为您的程序永远不会停止。

As a side note, you do not need to check all primes all the way to the last one you've discovered: you can stop once you reach the square root of the candidate prime, ie i*i > bag[k] . 作为旁注,你不需要检查所有素数到你发现的最后一个:你可以在到达候选素数的平方根时停止,即i*i > bag[k] You are not going to notice any effect when your limit is 100, but if you try 100000, it would help a lot more. 当你的限制为100时,你不会注意到任何效果,但如果你尝试100000,它会有更多帮助。

You have a number of logical mistakes in your code. 您的代码中存在许多逻辑错误。

First you have a for loop that has a finalization case unrelated to its indexer. 首先,你有一个for循环,其终结案例与其索引器无关。 While this is valid, it makes the code harder to understand. 虽然这是有效的,但它使代码更难理解。

More importantly, result is only ever set to false, not true, so thus the loop will run forever, as valid is never changed. 更重要的是,结果只会被设置为false,而不是true,因此循环将永远运行,因为有效永远不会改变。

Your logic for determining the first 100 prime numbers is incorrect. 您确定前100个素数的逻辑是不正确的。 And number of logical errors are present as indicated by others. 并且存在多个逻辑错误,如其他人所示。 I have re-written your code but not tested. 我重写了你的代码,但没有经过测试。 I guess it will work: 我猜它会起作用:

public class Trial02 
{   
    public static void main( String[] args) 
    {       
    int[] bag = new int[100];
    bag[0] = 2; //first element of prime array
    int valid = 1;
    int i;
    boolean isPrime = true;
    String str = "";

    //starting from 3 it checks if a number is prime until array is full
    for( i=3; valid<bag.length; i++)
    {
        isPrime = true;
        for (int k = 2; k < i; k++)
        {
            if (i % k == 0)
            {
                isPrime = false;
                break;
            }
        }

        if (isPrime == true)
        {
            bag[valid++] = i;
        }
    }

    //printing results
    for(i=0; i < bag.length; i++)
        str = str + bag[i] + " ";
    System.out.println("zaa xd");
    System.out.println(str);
    }

}

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

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