繁体   English   中英

使用C#编写的素数检查器

[英]Prime Number Checker Written in C#

我已经放弃了数字生成器,因为我知道它实际上是将每个数字分开,直到它收到一个素数。 我已经更改了代码,以便从指定范围"a" to "?" (in this case, 10)素数 Prime_2方法中的"a" to "?" (in this case, 10)Prime_2方法中被检查为素数。 然后,如果该数字是素数,则通过将布尔变量prime设置为true或false来返回Prime方法,但到目前为止,我只得到2为真,其余为假。 这显然不是真的。 因此,我感谢任何协助/意见/建议,使这个新计划可行。

public bool Prime(long num) // Prime method with a parameter for one number
{
    int div = 3; // what we divide by after checking if the number is divisible by 2
    bool prime = true; // set prime to true
    {
        for (long i = 0; i < 100 && prime == true; i++) // run 100 passes
        {
            if (num % 2 == 0 && num != 2) // if the number is divisible by 2 
            {                             // and is not 2, prime is false.
                prime = false;
            }
            else if (num % 2 != 0 && num != 2) // if the number is not divisible 
            {                                  // by 2 and the number is not 2...
                for (long x = 0; x <= 1000; x++) // then run 1000 passes of this:
                {
                    if (num % Math.Pow((div), x) == 0 && num != Math.Pow((div), x))
                    {   // if the number is divisible by our div to the power of x 
                        // and the number is not equal to div to the power of x, 
                        // prime is false.
                        prime = false;
                    }
                }
            }
        else   // otherwise add 2 to div making it the next consecutive odd number 
            {  // and run the pass again
                div = div + 2;
            }

        }
        return prime;
    }
}
public void Prime_2() // void Prime_2 method
{
    long a = 2; // starting number 2
    long b = 0; // set b

    Program prg = new Program(); //new instance of the Program class
    while (a <= 10)//the range a (2) - 10
    {
        b = a;//set "b" to "a" every time
        prg.Prime(b); // run the Prime method for numbers 2-10
        Console.WriteLine(b); // write the number being checked
        Console.WriteLine(prg.Prime(b)); // return if it is true or false for prime
        a++; // add 1 to a
    }
}
static void Main(string[] args)
{
    Program prog = new Program(); // instantiate a new Program
    prog.Prime_2(); // run the method, Prime_2
    Console.ReadLine(); // wait for input
}

如果你想通过试验分区来检查2到100之间的每个数字的原始性,这是你似乎要尝试做的,使用这个伪代码算法:

function isPrime(n)
    if n % 2 == 0
        return n == 2
    d := 3
    while d * d <= n
        if n % d == 0
            return False
        d := d + 2
    return True

这需要时间为O(n 1.5)用于找到素数多达n。 如果您想要更快的算法,请使用Eratosthenes的Sieve,即O(n log log n)

function primes(n)
    sieve := makeArray(2..n, True)
    for p from 2 to n step 1
        if sieve[p]
            output p
            for i from p*p to n step p
                sieve[i] := False

如果你对使用素数编程感兴趣,我谦虚地在我的博客上推荐这篇文章

我不确定你为什么要在你的功能中做你正在做的事情。 你能在代码中添加一些注释吗? 但是,检查素数的快速方法如下

bool IsPrime(int number) {
  if (number % 2 == 0 && number != 2) return false; // Don't check even numbers

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

当然,您应该在if语句中调用上面的函数,如果为true,则显示数字。

暂无
暂无

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

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