简体   繁体   English

打印从0到10,000的素数

[英]Print the prime numbers from 0 to 10,000

Im currently trying to create a program that prints the prime numbers from 0 to 10,000 using only for,do while and ifs. 我目前正在尝试创建一个程序,仅使用for,do while和ifs打印从0到10,000的素数。 I created this program but it doesn't runs 我创建了这个程序,但它没有运行

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();
        }

I don't know where the problem is and also if the for inside resets. 我不知道问题在哪里以及内部是否重置。

Try this with no bool variable!!!: 试试这个没有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();
}

Check Console.ReadKey(); 检查Console.ReadKey(); it should be after upper for loop, you can even change condition for upper for loot with <= since 10000 also need to check for prime condition. 它应该是后上for循环,你甚至可以改变条件上for战利品与<=10000还需要检查的prime条件。

Below is the efficient way to print prime numbers between 0 and 10000 下面是打印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);
            }
        }
    }
}

The first problem is that x % 1 will always be zero, at least for non-zero x . 第一个问题是x % 1始终为零,至少对于非零x You need to start the test (inner) loop at one and, for efficiency, stop when you've exceeded the square root of the number itself - if n has a factor f where f > sqrt(n) , you would have already found the factor n / f . 你需要在一个开始测试(内部)循环,为了效率,当你超过数字本身的平方根时停止 - 如果n有一个因子f ,其中f > sqrt(n) ,你就已经找到了因子n / f

The second problem is that you will write out a candidate number every time the remainder is non-zero. 第二个问题是,每当余数不为零时,您将写出候选编号。 So, because 15 % 4 is three, it will be output despite the fact that fifteen is very much a non-prime. 因此,因为15 % 4是3,所以尽管十五是非素数,但它仍将输出。 It will also be output at 15 % 2 , 15 % 4 , 15 % 6 , 15 % 7 , and so on. 它也将以15 % 2 15 % 4 15 % 6 15 % 7 ,依此类推。

The normal (naive) algorithm for prime testing is: 主要测试的正常(幼稚)算法是:

# 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

Is there any reason that you put Console.ReadKey(); 你有没有理由把Console.ReadKey(); inside of loop? 循环内部?

You should put that out of the loop unless press key during loop. 除非在循环期间按键,否则应将其置于循环外。

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();
}

And probably that code is just print lots of x. 并且可能该代码只是打印很多x。 You should to fix it. 你应该解决它。

Here is simple logic to Print Prime No for any upper limit. 对于任何上限,这是打印Prime No的简单逻辑。

Input : 10 Output : 2 , 3 , 5 ,7 输入: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();
        }
    }      
}

Here is my code where you can generate and print the prime numbers between two numbers (in between string_starting_number and string_last_number). 这是我的代码,您可以在其中生成和打印两个数字之间的素数(在string_starting_number和string_last_number之间)。 The lowest possible value for string_starting_number is 0 and the highest possible value for string_last_number is decimal.MaxValue-1=79228162514264337593543950334 and not 79228162514264337593543950335 because of the decimal_a++ command inside a for loop which will result to an overflow error. string_starting_number的最低可能值为0,string_last_number的最高可能值为dec​​imal.MaxValue-1 = 79228162514264337593543950334而不是79228162514264337593543950335,因为for循环中的decimal_a ++命令将导致溢出错误。

Take note that you should input the values in string type in string_starting_number and in string_last_number. 请注意,您应该在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();
        }
    }
}

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

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