C# - 检查限制中的素数

时间:2016-04-01 01:38:08

标签: c# primes

我想编写一个代码,显示第一个素数列表到另一个号码。例如从1到8,它将是2,3,5,7。我从&#34获得代码;检查数字是否是素数"由user1954418,因为我不知道从哪里开始,所以我对代码采用 NO 信用。

int num1;
Console.WriteLine("Prime Number:");
num1 = Convert.ToInt32(Console.ReadLine());
if (num1 == 0 || num1 == 1)
{
    Console.WriteLine(num1 + " is not prime number");
    Console.ReadLine();
}
else
{
    for (int a = 2; a <= num1 / 10; a++)
    {
        if (num1 % a == 0)
        {
            Console.WriteLine(num1 + " is not prime number");
            return;
        }
    }
    Console.WriteLine(num1 + " is a prime number");
    Console.ReadLine();
}

3 个答案:

答案 0 :(得分:2)

简单说明;以下代码将帮助您:

Console.WriteLine("Enter the Limit:");
int Limit;
if (!int.TryParse(Console.ReadLine(), out Limit))
   {
      Console.WriteLine("Invalid input");
   }
Console.WriteLine("List of prime numbers between 0 and {0} are :",Limit);
for (int i = 2; i < Limit; i++)
   {
      if (checkForPrime(i))
          Console.WriteLine(i);
   }
Console.ReadKey();

checkForPrime()的定义如下:

public static bool checkForPrime(int Number)
    {
        for (int a = 2; a <= Number / 2; a++)
        {
            if (Number % a == 0)
            {
                return false;
            }
        }
        return true;
    } 

答案 1 :(得分:2)

这是一个应该有用的示例。来自isPrime的代码我刚从Here

获得

然后在main函数中只有从你的起始编号到结束编号​​的循环,并在每个函数上运行isPrime函数。

以下是代码:

class Program
{
    static void Main(string[] args)
    {
        int numberStart;
        int numberEnd;

        // Take in the start point and the end point
        Console.WriteLine("Starting Number:");
        if(!int.TryParse(Console.ReadLine(), out numberStart)){
            Console.WriteLine("Your input is invalid.");
        }

        Console.WriteLine("Ending Number:");
        if (!int.TryParse(Console.ReadLine(), out numberEnd))
        {
            Console.WriteLine("Your input is invalid.");
        }

        // Loop from the first number to the last number, and check if each one is prime
        for (int number = numberStart; number < numberEnd; number++)
        {
            Console.WriteLine(number + " is prime?");
            Console.WriteLine(isPrime(number) + "\n");
        }



        Console.ReadLine();

    }

    // Function for checking if a given number is prime.
    public static bool isPrime(int number)
    {
        int boundary = (int) Math.Floor(Math.Sqrt(number));

        if (number == 1) return false;
        if (number == 2) return true;

        for (int i = 2; i <= boundary; ++i)
        {
            if (number % i == 0) return false;
        }

        return true;
    }
}

请注意,函数isPrime()仅检查根,因为没有必要按照用户从链接中提到的进一步检查。

希望有所帮助:)

答案 2 :(得分:1)

这是我在其中一个项目中编写/复制的一些代码。它可能会更顺畅,但它可以完成工作。

    // Found this awesome code at http://csharphelper.com/blog/2014/08/use-the-sieve-of-eratosthenes-to-find-prime-numbers-in-c/
    // This creates a List of Booleans where you can check if a value x is prime by simply doing if(is_prime[x]);
    public static bool[] MakeSieve(int max)
    {
        var sqrt = Math.Sqrt((double)max);
        // Make an array indicating whether numbers are prime.
        var isPrime = new bool[max + 1];
        for (var i = 2; i <= max; i++) isPrime[i] = true;

        // Cross out multiples.
        for (var i = 2; i <= sqrt; i++)
        {
            // See if i is prime.
            if (!isPrime[i]) continue;
            // Knock out multiples of i.
            for (var j = i * 2; j <= max; j += i)
                isPrime[j] = false;
        }
        return isPrime;
    }

    public static List<int> GetListOfPrimes(int max)
    {
        var isPrime = MakeSieve(max);
        return new List<int>(Enumerable.Range(1, max).Where(x => isPrime[x]));
        //var returnList = new List<int>();
        //for (int i = 0; i <= max; i++) if (isPrime[i]) returnList.Add(i);
        //return returnList;
    }

以下是一个使用示例:

    static void Main(string[] args)
    {
        var x = GetListOfPrimes(8);

        foreach (var y in x)
        {
            Console.WriteLine(y);
        }

        Console.Read();
    }