Prime Number Formula

前端 未结 13 1496
情话喂你
情话喂你 2020-12-06 08:47

I am trying to write a prime number function in C# and I am wondering if the follow code will work. It \"appears\" to work with the first 50 numbers or so. I just want to ma

相关标签:
13条回答
  • 2020-12-06 09:24

    You are apparently writing from a contrafactual dimension where 9 is a prime number, so I guess that our answers might not work for you. Two things though:

    1. Prime number generating functions are a non-trivial but exiting matter, the Wikipedia page is a good starter (http://en.wikipedia.org/wiki/Formula_for_primes)

    2. from (number%2!=0) it follows (number%4!=0). If you can't divide by 10, then you can't divide by 100 either.

    0 讨论(0)
  • 2020-12-06 09:26

    https://www.khanacademy.org/computing/computer-science/cryptography/comp-number-theory/a/trial-division

        public static bool isPrime(int number)
        {
            for (int k = 2; k <= Math.Ceiling(Math.Sqrt(number)); k++)
            {
                if (number > k && number % k == 0)
                    break;
                if (k >= Math.Ceiling(Math.Sqrt(number)) || number == k)
                {
                    return true;
                }
            }
            return false;
        }
    
    0 讨论(0)
  • 2020-12-06 09:27

    ExchangeCore Forums have a good bit of code that will pretty much let you generate any ulong number for primes. But basically here's the gist:

    int primesToFind = 1000;
    int[] primes = new int[primesToFind];
    int primesFound = 1;
    primes[0] = 2;
    for(int i = 3; i < int.MaxValue() && primesFound < primesToFind; i++)
    {
       bool isPrime = true;
       double sqrt = Math.sqrt(i);
       for(int j = 0; j<primesFound && primes[j] <= sqrt; j++)
       {
          if(i%primes[j] == 0)
          {
             isPrime = false;
             break;
          }
       }
       if(isPrime)
          primes[primesFound++] = i;
    }
    

    Once this code has finished running your primes will all be found in the primes array variable.

    0 讨论(0)
  • 2020-12-06 09:34

    It had to be done...

    public static bool IsPrime(this int number)
    {
        return (Enumerable.Range(1,number).Where(x => number % x == 0).Count() == 2);
    }
    
    0 讨论(0)
  • 2020-12-06 09:34

    This is a simple code for find prime number depend on your input.

      static void Main(string[] args)
        {
            String input = Console.ReadLine();
            long num = Convert.ToInt32(input);
            long a, b, c;
            c = 2;
            for(long i=3; i<=num; i++){
                b = 0;
                for (long j = 2; j < i ; j++) {
                    a = i % j;
                    if (a != 0) {
                        b = b+1;
                    }
                    else {
                        break;
                    }
                }
    
                if(b == i-2){
                    Console.WriteLine("{0}",i);
                }
            }
            Console.ReadLine();
        }
    
    0 讨论(0)
  • 2020-12-06 09:35

    Primality testing is the way to go, but in case you want a quick and dirty hack, here's something.

    If it's not working fast enough, you can build a class around it and store the PrimeNumbers collection from call to call, rather than repopulating it for each call.

        public bool IsPrime(int val)
        {
            Collection<int> PrimeNumbers = new Collection<int>();
            int CheckNumber = 5;
            bool divisible = true;
            PrimeNumbers.Add(2);
            PrimeNumbers.Add(3);
    
            // Populating the Prime Number Collection
            while (CheckNumber < val)
            {
                foreach (int i in PrimeNumbers)
                {
                    if (CheckNumber % i == 0)
                    {
                        divisible = false;
                        break;
                    }
                    if (i * i > CheckNumber) { break; }
                }
                if (divisible == true) { PrimeNumbers.Add(CheckNumber); }
                else { divisible = true; }
                CheckNumber += 2;
            }
            foreach (int i in PrimeNumbers)
            {
                if (CheckNumber % i == 0)
                {
                    divisible = false;
                    break;
                }
                if (i * i > CheckNumber) { break; }
            }
            if (divisible == true) { PrimeNumbers.Add(CheckNumber); }
            else { divisible = true; }
    
            // Use the Prime Number Collection to determine if val is prime
            foreach (int i in PrimeNumbers)
            {
                if (val % i == 0) { return false; }
                if (i * i > val) { return true; }
            }
            // Shouldn't ever get here, but needed to build properly.
            return true;
        }
    
    0 讨论(0)
提交回复
热议问题