-4

I'm trying to do a Prime Number finder but cant see why it's not working. When i run the debug test nothing show in the console. Could someone check it and tell me what i do wrong?

  List<int> primes = new List<int>();
        primes.Add(2);
        primes.Add(3);
        int maxPrime = 11;                //The maximum found Primes
        int primeCount = primes.Count;     //Current Number of Primes
        int num = 4;                    //Current Number
        int x = 0;                 //
        int curPrime = primes[x];

        while (primeCount < maxPrime)
        {
            if (x != primeCount)
            {
                if (num % primes[x] == 0)
                {
                    num++;
                    x = 0;
                }
                else
                {
                    x++; 
                }
            }

            else
            {
                primes.Add(num);
                primeCount=primes.Count;
                x = 0;
            }
        }
        primes.ForEach(i => Console.Write("{0}\t", i));
DeathUili
  • 1
  • 1
  • 1
    When you step through the code in the debugger, where *specifically* does the observed behavior differ from the expected behavior? What are the runtime values when that happens? – David Jan 19 '16 at 17:24
  • there are so00000000 Many working examples on how to do this here is an even simpler example doing it with a single value http://stackoverflow.com/questions/886540/prime-numbers-c-sharp – MethodMan Jan 19 '16 at 17:32
  • 1
    Increasing `primeCount` would help.... – Alex Jan 19 '16 at 17:34

1 Answers1

1

You have an infinite loop.

Since you never modify primeCount or maxPrime, this will always be true:

while (primeCount < maxPrime)

In order to end that loop, you need to modify one of those two values in such a way that the condition will evaluate to false.

(Note: There appear to also be other bugs/problems in the code aside from this. For example, num = num++; doesn't do what you probably think it does.)

David
  • 208,112
  • 36
  • 198
  • 279