-1

So I am trying to make something to figure out prime numbers but my program thinks EVERYTHING is not prime. Does anyone know why? EDIT I guess I should have elaborated more on that I was trying to accomplish, I have three vars, two witch it multiplies together and the if questions ask if it is greater than C if this was the situation it would write it down as prime (I already see some mistakes made I am fixing it) And if at some point in time A times B equaled C (C Being the number we are trying to figure out is prime or not) It would be written as prime

namespace ConsoleApplication3
{
    class Program
    {
        const string fileName = "NotPrime.txt";
        const string filnamePrime = "Prime.txt";
        static void Main(string[] args)
        {
            var c = 2;
            var a = 1;
            var b = 1;
            while (true)
            {
                if (a * b == 1)
                {
                    a = a + 1;
                }

                if ((a * b == c) && (c != 1))
                {
                    Console.WriteLine("{0} is not prime.", c);
                    using (BinaryWriter writer = new BinaryWriter(File.Open(fileName, FileMode.Create)))
                    {
                        writer.Write(string.Format("{0}\r\n", c));
                    }
                    a = a = 1;
                    b = b = 1;
                    c = c + 1;
                }

                if ((a * b != c) && (c * b > c))
                {
                    Console.WriteLine("{0} is prime.", c);
                    using (BinaryWriter writesr = new BinaryWriter(File.Open(filnamePrime, FileMode.Create)))
                    {
                        writesr.Write(string.Format("{0}\r\n", c));
                    }
                    a = a = 1;
                    b = b = 1;
                    c = c + 1;
                }

                if (a * b < c)
                {
                    b = b + 1;
                    a = a = 1; // This is causing everything to mess up here but I know it will be nessesary once I figure out how to fix this current error.
                }
                Thread.Sleep(1000);

            }
        }
    }
}
IOLobster
  • 13
  • 1
  • 2
    What are you trying to acomplish by `a = a = 1; b = b = 1; c = c + 1;` ? Also, would you care to elaborate on your algorithm a little bit? I am somewhat confused. – MaLiN2223 Feb 04 '17 at 03:28
  • When do you ever change `b`? Anyway, try writing a separate function to test whether a number is prime, and move from there. – Ry- Feb 04 '17 at 03:41
  • 1
    Put some parentheses around those boolean expression pieces so that you can be sure which part of them is executing first, as in `if ((a * b == c) && (c != 1))` and `if ((a * b != c) && (c * b > c))`. Use the `&&` operator. – Robert Harvey Feb 04 '17 at 03:46
  • I'm having trouble even following your algorithm's logic. Could you describe the process you are trying to do? – Abion47 Feb 04 '17 at 03:47
  • Like Ryan said, you don't ever change `b`. That means you will only ever be checking `a * 1`, which will never tell you whether a number is prime. – Abion47 Feb 04 '17 at 04:57

1 Answers1

-2

I think that you need to change the single & to &&. A single & is a bitwise and operator which has a higher order order of operation that most other operators. This means that it happens before the == or != happens. You may also want to put parenthesis around the expressions that you are "anding" together. That way there is no confusion about what you are intending.

Mike Wodarczyk
  • 1,247
  • 13
  • 18
  • 1
    *This means that it happens before the == or != happens.* That isn’t correct for C#: https://msdn.microsoft.com/en-us/library/aa691323(v=vs.71).aspx – Ry- Feb 04 '17 at 03:38
  • 1
    This is *a* problem, but it is not *the* problem. Changing every instance of `&` to `&&` makes it so *no* numbers are marked as prime. – Abion47 Feb 04 '17 at 04:42
  • @Abion47: That was already the behaviour. `&` and `&&` both have lower precedence than `==` and the program doesn’t rely on short-circuiting. – Ry- Feb 04 '17 at 05:19