简体   繁体   English

得到所有素数

[英]getting all prime numbers

I wrote a simple C# console application that gets all prime numbers, and stacks them into a string variable. 我编写了一个简单的C#控制台应用程序,该应用程序获取所有素数,并将其堆叠到字符串变量中。

My question is why does the first "if" accept all the numbers? 我的问题是,为什么第一个“如果”接受所有数字? I think the rule I wrote is fine. 我认为我写的规则很好。 My code (of course all the variables are formatted in the head of the script): 我的代码(当然所有变量的格式都在脚本的开头):

check++;
number++;
num = number;
if (number / num == 1 && number / 1 == num)
{
    Console.WriteLine("Working...");
    Console.WriteLine("Numbers Checked: " + check);

    produce++;
    Console.WriteLine("Numbers Prodused: " + produce);


    Console.WriteLine("Numbers Failed : " + fail);

    System.Threading.Thread.Sleep(1);
    Console.Clear();

    //storing the numbers
    str += Convert.ToString(number);
}
else
{
    fail++;
}
Console.WriteLine(str);

The problem is that when I run the program, the fail indicator stays on 0 which means the loop is processing all the numbers with no exception for the prime numbers. 问题是,当我运行程序时,失败指示符保持为0,这意味着循环正在处理所有数字,质数没有例外。

How do I fix that? 我该如何解决? Is my "if (rule)" wrong? 我的“如果(规则)”是错误的吗?


EDIT ! 编辑! :

i started fresh ;) here's what i did: 我刚开始;)这就是我所做的:

        bool isPrime = false;
        int number = 0, counter = 0;

        while (number <= counter)
        {
            counter++;
            number++;
            isPrime = true;
            for (int i = 2; i < number; ++i)
            {
                if (number % i == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
            {
                System.Threading.Thread.Sleep(1);
                Console.WriteLine(number);
                Console.Beep();
            }
            Console.WriteLine("Try Number " + counter);

works great, I'll continue developing it into a windows forms application soon ;) 效果很好,我将继续将其开发为Windows窗体应用程序;)

 num = number;
        if (number / num == 1 && number / 1 == num)
        {

Let's assume number is equal to 10 . 假设number等于10

num = number = 10;

if(10 / 10 == 1 && 10 / 1 == 10) {
    // This is called a constant positive.
}

num and number are always the same, so we could say in a more mathematical sense numnumber始终相同,因此我们可以从数学上讲

for all a,b where a == b.

a/b = 1
a/1 = b

This means that no matter the value of number , you're always going to evaluate to true in your if statement . 这意味着,无论价值number ,你总是会评估为true在你的if statement

What you want to do 你想做什么

You should look a little closer into how you define a prime number . 您应该更仔细地了解如何定义质数 At the moment you're saying the following: 目前,您要说的是:

Some integer, a, is considered prime if and only if it can be divided by 1 and itself. 当且仅当可以将整数除以1及其本身时,某些整数a 才被视为素数。

You also need to prove that it can't be divided by numbers less than it too! 您还需要证明它也不能被数字除以小于!

so.. 所以..

Whereas the approach you can take is the certain method, where you check all 0 << i << a (between 0 and the current number), or you can use some of the more interesting methods which will work out if the number is a probabilistic prime . 可以采用的方法是特定的方法,即检查所有 0 << i << a (在0和当前数字之间),或者可以使用一些更有趣的方法,如果数字为a,则可以解决该问题。 probabilistic prime That is, we're not certain that it is a prime number, but there is a very high chance that it is. 也就是说,我们certain它是质数,但是很有可能是质数。

For certain primes, check this , and when you are feeling more confident try this . 对于某些素数,请检查一下 ,当您更有信心时,请尝试一下

For probabilistic primes, here . 对于概率素数,请在此处 This will give you an explanation of many different algorithms. 这将为您解释许多不同的算法。

This makes absolutely no sense. 这绝对没有道理。 Let's break down these lines: 让我们分解以下几行:

num = number;
if (number / num == 1 && number / 1 == num)

Since you set num = number , number / num will always be 1, so the left side evaluates to true always . 由于您设置num = numbernumber / num 始终为1,因此左侧的值始终为true。

The right part doesn't even make sense. 正确的部分甚至没有任何意义。 Dividing by 1 does absolutely nothing, so number / 1 is just number . 除以1绝对不会做任何事情,因此number / 1只是number Now, all you really have is number == num , and since you set num and number as the same, that also evaluates to true. 现在,您真正拥有的只是number == num ,并且由于您将numnumber设置为相同,因此其结果也为true。

You might as well just write if (true) . 您最好只写if (true)

What exactly are you trying to accomplish with that if statement? 您到底想用if语句完成什么?

Because every number divided by 1 is the number it self. 因为每个数字除以1就是它自己的数字。 and Why number and num are equal ? 为什么数字和num相等? because the row before the if condition assign num as number . 因为if条件之前的行将num分配为number

num = number; num =数字; you are setting num equal to number each time before if condition is applied 如果应用条件,则每次设置num等于number

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

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