Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 程序中查找素数的循环出现问题_C#_Loops_Console Application_Nested Loops - Fatal编程技术网

C# 程序中查找素数的循环出现问题

C# 程序中查找素数的循环出现问题,c#,loops,console-application,nested-loops,C#,Loops,Console Application,Nested Loops,嗨,我一直有一个问题,如何让这个工作。它是 用于使用用户输入来设置x来查找第一个x个素数。然后,在打印结果之前,它会将任何结果放入数组中。我想我已经对循环提出了一个问题,因为一旦输入了用户输入,就不会发生任何事情。如果有人能指出我哪里出了问题,并提出如何解决的建议,我将不胜感激。谢谢 static void Main(string[] args) { int max, i, count; i = 0; // this is used to keep trc

嗨,我一直有一个问题,如何让这个工作。它是 用于使用用户输入来设置x来查找第一个x个素数。然后,在打印结果之前,它会将任何结果放入数组中。我想我已经对循环提出了一个问题,因为一旦输入了用户输入,就不会发生任何事情。如果有人能指出我哪里出了问题,并提出如何解决的建议,我将不胜感激。谢谢

static void Main(string[] args)
    {
        int max, i, count;
        i = 0; // this is used to keep trck of how many primes have been added to the array
        count = 0; // this is used to test each number
        Console.WriteLine("This will work out the first x prime numbers with x being the number of prime numbers you want");
        Console.WriteLine("Enter the number of prime numbers you want.");
        max = Convert.ToInt32(Console.ReadLine());

        int[] primes = new int[max];

        while (i <= max)
        {
         while (count <= 9999)
         {
             if (count % 2 == 0 || count % 3 == 0 || count % 5 == 0 || count % 7 == 0 ) // tests if count number is a prime 
             {
                 if (count == 2 || count == 3 ||count == 5 ||count == 7 ) // ensures 2,3,5,7 are added to primes if neccesarry
                 {

                     primes[count] = count; //add to array
                     i++; // increments the count on the number of prime numbers
                 }
                 count ++; // increments the count 
                 break;
             }
             else
             {
                 primes[count] = count;
                 i++;
                 count ++;
             }
         }
        }

        Console.WriteLine("The first {0} prime numbers are ... ", max);
        foreach(var item in primes)
        {
            Console.Write(item.ToString() + ", ");
        }
    }
static void Main(字符串[]args)
{
int max,i,count;
i=0;//用于记录已向数组中添加了多少素数
count=0;//用于测试每个数字
WriteLine(“这将计算出前x个素数,其中x是您想要的素数”);
WriteLine(“输入您想要的素数的数目。”);
max=Convert.ToInt32(Console.ReadLine());
int[]素数=新的int[max];

虽然(i首先,这不是测试一个数字是否是素数的方法,但同样,循环是错误的

如果你输入的最大值大于小于9999的素数,它将永远在外循环中循环

查看wiki以获得一些初步见解:


为了检查我的陈述,您可以在外部循环中添加一些控制台输出,并根据尝试保持原始代码的精神亲自查看,以下是对您的算法的调整,修改为使用:

  • 筛可以迭代地确定所有素数,只有知道2是素数
  • 内环上的退出条件应该是当we超过我们正在测试的数字的平方根时,使用我们的已知素数列表(不是
    9999
    break
    )。由于素数的定义,素数列表最多为X,我们可以解析所有其他素数最多为X*X。(因此,如果我们超过平方根,我们可以停止测试,例如2可以解析3和4,2和3可以解析9,等等)。这也是因为我们的素数已经按升序排序
  • 我保留了原始变量,即,
    count
    是下一个测试的候选素数,
    I
    是素数的当前索引,
    max
    是填充的上限
  • 第一个素数2需要硬编码(即
    素数[0]=2
    ),下一个素数索引
    i
    将为1,之后我们需要检查的第一个候选数将为3

//我们确实需要硬编码2是一个素数。其他一切都可以派生出来
素数[0]=2;
i=1;
计数=3;
//即,当我们得到所需数量的素数时停止
而(i计数)
打破
//如果候选人的号码
if(计数%primes[primeIndex]==0)
foundFactor=true;
primeIndex++;
}
如果(!foundFactor)
{
素数[i]=计数;
i++;
}
计数++;
}

根据@Massimo的链接,上述暴力机制效率极低。一个改进是维基文章中引用的“6轮”——在
count=6
之后,6轮将每一组候选数字分为6批进行测试,所有2和3的倍数都可以立即消除。其他较大的轮也可以使用计算出当你测试121(=11*11)时会发生什么。它不能被2,3,5或7整除。。。
// We do need to hard code that 2 is a prime number. Everything else can be derived
primes[0] = 2;
i = 1;
count = 3;

// i.e. stop when we get the required number of primes
while (i < max)
{
    // Indicator to stop testing this candidate if we find a factor
    bool foundFactor = false;
    // Start off each primality test from the start of our primes array
    var primeIndex = 0;

    // Try and find a factor of 'count' with our known primes
    while (!foundFactor && primeIndex < i)
    {
        // We can stop after the square root. Multiplication is faster than sqrt
        if (primes[primeIndex]*primes[primeIndex] > count)
            break;
        // If the candidate number
        if (count % primes[primeIndex] == 0)
            foundFactor = true;
        primeIndex++;
    }
    if (!foundFactor)
    {
        primes[i] = count;
        i++;
    }

    count++;
}