简体   繁体   English

查找下一个素数

[英]Find next Prime Number

I am trying to find the next prime number after the number the user enters in. 我正在尝试在用户输入号码后找到下一个素数。

Here is the code I have so far: 这是我到目前为止的代码:

public int Calculation(int number)
{
    //set the isPrime to false
    bool isPrime = false;

    //do this while isPrime is still false
    do
    {
        //increment the number by 1 each time
        number = number + 1;

        int squaredNumber = (int)Math.Sqrt(number);

        //start at 2 and increment by 1 until it gets to the squared number
        for (int i = 2; i <= squaredNumber; i++)
        {
            //how do I check all i's?
            if (number % i != 0)
            {
                isPrime = true;
            }


        }


    } while (isPrime == false);

    //return the prime number
    return number;
}

I know something is missing because the first time i gives a remainder that is NOT 0 then it returns that number as prime. 我知道丢失了一些东西,因为我第一次给出的余数不是0,那么它将返回该数作为质数。 The problem is I can't figure out the logic/syntax to see if every i in that loop is NOT 0 as remainder. 问题是我无法弄清楚逻辑/语法来查看该循环中的每个i是否都不为0。

There are better ways to find prime numbers, but keeping in line with your algorithm, what you want to do is start with isPrime = true; 有更好的方法来查找素数,但为了与算法保持一致,要执行的操作是从isPrime = true;开始isPrime = true; , then set it to false if there are any i where the remainder is 0. You can also break out of the loop at that point. ,然后将其设置为false ,如果有任何i在余数是0。你也可以break在这一点圈外。

So a revised version: 所以是一个修订版:

public int Calculation(int number)
{    
    while(true)
    {
        bool isPrime = true;
        //increment the number by 1 each time
        number = number + 1;

        int squaredNumber = (int)Math.Sqrt(number);

        //start at 2 and increment by 1 until it gets to the squared number
        for (int i = 2; i <= squaredNumber; i++)
        {
            //how do I check all i's?
            if (number % i == 0)
            {
                isPrime = false;
                break;
            }
        }
        if(isPrime)
            return number;
    }
}

If you use a BitArray as a Sieve of Eratosthenes you won't need a loop to test for prime. 如果将BitArray用作Eratosthenes的筛网,则不需要循环即可测试素数。 The value of the BitArray at that index will be true or false according to whether it's prime. 根据它的质数,在该索引处的BitArray的值为true或false。

A function like this will produce the Bitarray: 这样的函数将产生Bitarray:

public static BitArray ESieve(int upperLimit)
{
    int sieveBound = (int)(upperLimit - 1);
    int upperSqrt = (int)Math.Sqrt(sieveBound);
    BitArray PrimeBits = new BitArray(sieveBound + 1, true);
    PrimeBits[0] = false;
    PrimeBits[1] = false;
    for(int j = 4; j <= sieveBound; j += 2)
    {
        PrimeBits[j] = false;
    }
    for(int i = 3; i <= upperSqrt; i += 2)
    {
        if(PrimeBits[i])
        {
            int inc = i * 2;
            for(int j = i * i; j <= sieveBound; j += inc)
            {
                PrimeBits[j] = false;                       
            }
        }
    }
    return PrimeBits;
}

Declare the Bitarray: 声明Bitarray:

BitArray IsPrime = ESieve(1000000);

Finding the next prime is a simple matter of iterating through the bitarray to find the next one set to true: 找到下一个素数很简单,只需遍历位数组以找到下一个设置为true的问题:

int FindNextPrime(int number)
{
    number++;
    for(; number < IsPrime.Length; number++)
        //found a prime return that number
        if(IsPrime[number])
            return number;
    //no prime return error code
    return -1;
}
if ((number % i) != 0)
{
    isPrime = true;
}

You need to wrap number % i in parens due to % having lower operator precedence to !=. 由于%的运算符优先级比!=低,因此您需要将数字%i包裹在括号中。 I didn't test the correctness of the rest of your logic, but an input of 5 correctly returns 7 as the next prime. 我没有测试其余逻辑的正确性,但是输入5会正确返回7作为下一个质数。

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

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