1

I wrote a simple C# console application that gets all prime numbers, and stacks them into a string variable.

My question is why does the first "if" accept all the numbers? I think the rule I wrote is fine. My code (of course all the variables are formatted in the head of the script):

check++;
number++;
num = number;
if (number / num == 1 && number / 1 == num)
{
    Console.WriteLine("Working...");
    Console.WriteLine("Numbers Checked: " + check);

    produce++;
    Console.WriteLine("Numbers Prodused: " + produce);


    Console.WriteLine("Numbers Failed : " + fail);

    System.Threading.Thread.Sleep(1);
    Console.Clear();

    //storing the numbers
    str += Convert.ToString(number);
}
else
{
    fail++;
}
Console.WriteLine(str);

The problem is that when I run the program, the fail indicator stays on 0 which means the loop is processing all the numbers with no exception for the prime numbers.

How do I fix that? Is my "if (rule)" wrong?


EDIT ! :

i started fresh ;) here's what i did:

        bool isPrime = false;
        int number = 0, counter = 0;

        while (number <= counter)
        {
            counter++;
            number++;
            isPrime = true;
            for (int i = 2; i < number; ++i)
            {
                if (number % i == 0)
                {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime)
            {
                System.Threading.Thread.Sleep(1);
                Console.WriteLine(number);
                Console.Beep();
            }
            Console.WriteLine("Try Number " + counter);

works great, I'll continue developing it into a windows forms application soon ;)

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3107990
  • 147
  • 1
  • 10

4 Answers4

4
 num = number;
        if (number / num == 1 && number / 1 == num)
        {

Let's assume number is equal to 10.

num = number = 10;

if(10 / 10 == 1 && 10 / 1 == 10) {
    // This is called a constant positive.
}

num and number are always the same, so we could say in a more mathematical sense

for all a,b where a == b.

a/b = 1
a/1 = b

This means that no matter the value of number, you're always going to evaluate to true in your if statement.

What you want to do

You should look a little closer into how you define a prime number. At the moment you're saying the following:

Some integer, a, is considered prime if and only if it can be divided by 1 and itself.

You also need to prove that it can't be divided by numbers less than it too!

so..

Whereas the approach you can take is the certain method, where you check all 0 << i << a (between 0 and the current number), or you can use some of the more interesting methods which will work out if the number is a probabilistic prime. That is, we're not certain that it is a prime number, but there is a very high chance that it is.

For certain primes, check this, and when you are feeling more confident try this.

For probabilistic primes, here. This will give you an explanation of many different algorithms.

Community
  • 1
  • 1
christopher
  • 26,815
  • 5
  • 55
  • 89
3

This makes absolutely no sense. Let's break down these lines:

num = number;
if (number / num == 1 && number / 1 == num)

Since you set num = number, number / num will always be 1, so the left side evaluates to true always.

The right part doesn't even make sense. Dividing by 1 does absolutely nothing, so number / 1 is just number. Now, all you really have is number == num, and since you set num and number as the same, that also evaluates to true.

You might as well just write if (true).

What exactly are you trying to accomplish with that if statement?

tnw
  • 13,521
  • 15
  • 70
  • 111
0

Because every number divided by 1 is the number it self. and Why number and num are equal ? because the row before the if condition assign num as number.

Orel Eraki
  • 11,940
  • 3
  • 28
  • 36
0

num = number; you are setting num equal to number each time before if condition is applied

Anoop Kanyan
  • 618
  • 7
  • 19