小编Ben*_*nes的帖子

试图找到第 10001 个质数,c#

我正在尝试“找到第 10001 个素数”作为欧拉项目挑战的一部分,但我不知道为什么我的代码不起作用。当我测试我的 isPrime() 函数时,它成功地找到了一个数字是否是素数,但我的程序返回 10200 作为第 10001 个素数。为什么是这样?

这是我的代码:

using System;
using System.Collections.Generic;

namespace Challenge_7
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Solution to Project Euler");
            Console.WriteLine("Challenge 7 - Find the 10001st prime");

            Console.WriteLine("\nProject Start:");

            List<int> primes = new List<int>();

            int number = 1;
            while (primes.Count != 10001)
            {
                if (isPrime(number))
                {
                    primes.Add(number);
                    Console.WriteLine(number);
                }

                number++;
            }

            Console.WriteLine("The 10001st prime is: {0}", primes[10000]);

            Console.ReadLine();
        }

        private static bool isPrime(int n)
        {
            bool prime = true;

            for (int …
Run Code Online (Sandbox Code Playgroud)

c# primes

0
推荐指数
1
解决办法
598
查看次数

标签 统计

c# ×1

primes ×1