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

时间:2013-08-03 14:14:14

标签: c# algorithm primes

我已经放弃了号码生成器,因为我知道它实际上是将每个号码分开,直到它收到一个素数。我更改了代码,以便在"a" to "?" (in this case, 10)方法中指定范围Prime_2的素数在Prime方法中被检查为素数。然后,如果该数字是素数,则通过将布尔变量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 个答案:

答案 0 :(得分:8)

如果你想通过试验分区检查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

如果您对使用素数进行编程感兴趣,我会在我的博客上谦虚地推荐这个essay

答案 1 :(得分:0)

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

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,则显示数字。