打印从 0 到 10,000 的素数

Print the prime numbers from 0 to 10,000

我目前正在尝试创建一个程序,仅使用 for、do while 和 ifs 打印 0 到 10,000 之间的质数。我创建了这个程序,但它没有运行

static void Main(string[] args)
    { 
        for (int x = 2; x < 10000; x++)
        { 
            for (int y = 1; y < x; y++)
            { 
                if (x % y != 0)
                {
                    Console.WriteLine(x); 
                }  
            }
            Console.ReadKey();
        }

我不知道问题出在哪里,也不知道里面的for是否重置了。

第一个问题是 x % 1 总是 为零,至少对于非零 x。您需要从一个开始测试(内部)循环,并且为了提高效率,当您超过数字本身的平方根时停止 - 如果 n 有一个因子 f 其中 f > sqrt(n), 你应该已经找到因子 n / f.

第二个问题是,每次余数不为零,你都会写出一个候选项。因此,因为 15 % 4 是三,所以尽管十五是非常非素数的事实,它仍将被输出。也会在15 % 215 % 415 % 615 % 7等处输出。

素数测试的正常(朴素)算法是:

# All numbers to test.

foreach number 2..whatever:
    # Assume prime, check all numbers up to squareroot(number).

    isPrime = true
    foreach test 2..infinity until test * test > number:
        # If a multiple, flag as composite and stop inner loop.

        if number % test == 0:
            isPrime = false
            exit foreach
        end
    end

    # If never flagged as composite, output as prime.

    if isPrime:
        output number
end

在没有 bool 变量的情况下试试这个!!!:

static void Main(string[] args)
{
    for (int x = 2; x < 10000; x++)
    {
        int isPrime = 0;
        for (int y = 1; y < x; y++)
        {
            if (x % y == 0)
                isPrime++;

            if(isPrime == 2) break;
        }
        if(isPrime != 2)
           Console.WriteLine(x);

        isPrime = 0;
    }
    Console.ReadKey();
}

检查Console.ReadKey();它应该在上for循环之后,你甚至可以用<=改变上for战利品的条件,因为10000也需要检查 prime 条件。

你有什么理由把Console.ReadKey();循环内部?

除非在循环期间按下键,否则您应该将其置于循环之外。

static void Main(string[] args)
{
    for (int x = 2; x < 10000; x++)
    {

        for (int y = 1; y < x; y++)
        {

            if (x % y != 0)
            {
                Console.WriteLine(x);
            }
        }
    }
    Console.ReadKey();
}

并且可能该代码只是打印了很多 x。 你应该修复它。

下面是打印 0 到 10000 之间质数的有效方法

using System.IO;
using System;

class Program
{
    static void Main()
    {
        Console.WriteLine("Below are prime numbers between 0 and 10000!");
        Console.WriteLine(2);
        for(int i=3;i<=10000;i++)
        {
            bool isPrime=true;
            for(int j=2;j<=Math.Sqrt(i);j++)
            {
                if(i%j==0)
                {
                    isPrime=false;
                    break;
                }
            }
            if(isPrime)
            {
                Console.WriteLine(i);
            }
        }
    }
}

这是打印任何上限的素数的简单逻辑。

输入:10 输出:2、3、5、7

namespace PurushLogics
{
    class Purush_PrimeNos
    {
        static void Main()
        {
            //Prime No Program
            bool isPrime = true;
            Console.WriteLine("Enter till which number you would like print Prime Nos\n");
            int n = int.Parse(Console.ReadLine());
            Console.WriteLine("Prime Numbers : ");
            for (int i = 2; i <= n; i++)
            {
                for (int j = 2; j <= n; j++)
                {

                    if (i != j && i % j == 0)
                    {
                        isPrime = false;
                        break;
                    }

                }
                if (isPrime)
                {
                    Console.Write("\t" + i);
                }
               isPrime = true;
            }
            Console.ReadKey();
        }
    }      
}

这是我的代码,您可以在其中生成和打印两个数字之间的质数(在 string_starting_number 和 string_last_number 之间)。 string_starting_number 的最低可能值为 0,string_last_number 的最高可能值为 decimal.MaxValue-1=79228162514264337593543950334 而不是 79228162514264337593543950335 因为 for 中的 decimal_a++ 命令将导致溢出错误的循环。

注意在string_starting_number和string_last_number.

中输入字符串类型的值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GeneratingPrimeNumbers
{
    class Program
    {
        static void Main(string[] args)
        {
            string string_starting_number = "1"; //input here your choice of starting number
            string string_last_number = "10"; //input here your choice of last number
            decimal decimal_starting_number = Convert.ToDecimal(string_starting_number);
            decimal decimal_last_number = Convert.ToDecimal(string_last_number);
            string primenumbers = "";
            ulong ulong_b;
            ulong ulong_c;
            if (decimal_starting_number <= ulong.MaxValue)
            {
                ulong ulong_starting_number = Convert.ToUInt64(decimal_starting_number);
                ulong ulong_last_number;
                if (decimal_last_number > ulong.MaxValue)
                {
                    ulong_last_number = ulong.MaxValue;
                }
                else
                {
                    ulong_last_number = Convert.ToUInt64(decimal_last_number);
                }
                if (ulong_starting_number == 0 || ulong_starting_number == 1 || ulong_starting_number == 2 || ulong_starting_number == 3)
                {
                    primenumbers = 2 + " " + 3;
                    ulong_starting_number = 5;
                }
                if (ulong_starting_number % 2 == 0)
                {
                    ulong_starting_number++;
                }
                ulong ulong_a;
                for (ulong_a = ulong_starting_number; ulong_a <= ulong_last_number; ulong_a += 2)
                {
                    ulong_b = Convert.ToUInt64(Math.Ceiling(Math.Sqrt(ulong_a)));
                    for (ulong_c = 3; ulong_c <= ulong_b; ulong_c += 2)
                    {
                        if (ulong_a % ulong_c == 0)
                        {
                            goto next_value_of_ulong_a;
                        }
                    }
                    primenumbers = primenumbers + " " + ulong_a;
                    next_value_of_ulong_a:
                    {
                    }
                }
            }
            if (decimal_last_number > ulong.MaxValue)
            {
                string ulong_maximum_value_plus_two = "18446744073709551617";
                if (decimal_starting_number <= ulong.MaxValue)
                {
                    decimal_starting_number = Convert.ToDecimal(ulong_maximum_value_plus_two);
                }
                if (decimal_starting_number % 2 == 0)
                {
                    decimal_starting_number++;
                }
                decimal decimal_a;
                for (decimal_a = decimal_starting_number; decimal_a <= decimal_last_number; decimal_a += 2)
                {
                    ulong_b = Convert.ToUInt64(Math.Ceiling(Math.Sqrt(ulong.MaxValue) * Math.Sqrt(Convert.ToDouble(decimal_a / ulong.MaxValue))));
                    for (ulong_c = 3; ulong_c <= ulong_b; ulong_c += 2)
                    {
                        if (decimal_a % ulong_c == 0)
                        {
                            goto next_value_of_decimal_a;
                        }
                    }
                    primenumbers = primenumbers + " " + decimal_a;
                    next_value_of_decimal_a:
                    {
                    }
                }
            }
            Console.WriteLine(primenumbers);
            Console.ReadKey();
        }
    }
}