Hello! Task: Find the number of all prime numbers in the range from 0 to 1000. Help modify the code. I do not understand what is missing.

class Program { static void Main(string[] args) { int result = 0; for (int i = 2; i <= 1000; i++) { if (i % i == 0 & i / 1 == 0) { result += 1; } } Console.WriteLine("Простых чисел" + result); Console.ReadKey(); } } 
  • The same problem: I do not understand what you do not like about him. - Vladimir Martyanov
  • only 0 when divided by 1 will return 0 - Grundy
  • so you end up with only the number of numbers, and you seem to want to get the numbers themselves .. and the very condition is strange - I. Smirnov
  • I apologize, incorrectly described the task. I need a quantity. As a result, it outputs 0. - Riccu

3 answers 3

A prime number is a natural (positive integer) number that has exactly two different natural dividers - a unit and itself.

To programmatically check a simple number or not, it is enough to check whether this number is not divisible by numbers up to the square root of this number. If divisible - the number is not simple, otherwise - simple.

To determine whether a number is simple, we write a method that defines a prime number or not:

 private static bool IsPrimeNumber(int number) { int sqrtNumber = (int) (Math.Sqrt(number)); for (int i = 2; i <= sqrtNumber; i++) { if (number % i == 0) return false; } return true; } 

Now, actually, the code itself, in which we will call the method:

 int count = 0; int maxNumber = 1000;// максимальное число, до которого будем считать (можно менять) // можно переделать на ввод с клавиатуры for (int num = 2; i <= maxNumber; num++) { if (IsPrimeNumber(num)) { count++; // Console.Write(num.ToString()+","); // можно вывести на экран } } // Console.WriteLine(); // отступ просто для красоты, если выводим на экран Console.WriteLine("Количество простых чисел в диапазоне от 0 до " + maxNumber); Console.WriteLine("Простых чисел: " + count); 

Result: enter image description here

In your case, the condition is not correct. Here is a cognitive page: How to check if a number is simple

  • Oh, tell me how to call a program in the console outside of Eclipse. - Artik Slayer
  • +1, but in English Prime, not Simple. - VladD
  • one
    I think that you just run the executable file .exe , which was the result. - Denis Bubnov
  • @VladD, thanks, I will know. - Denis Bubnov
  • one
    It seems not to half, but to the square root of the number - Grundy

The solution has already been written, nevertheless I think that in this case it makes sense to analyze the author’s mistake in detail.

So, the initial condition proposed by the author looks like this.

 if (i % i == 0 & i / 1 == 0) 

Let's break it into pieces:

  1. i % i == 0 This condition is always true, since the remainder of dividing any number by the same number is always 0.

  2. i / 1 == 0 . This condition is never satisfied, since the result of dividing any number by 1 is always equal to that number, but not like 0. It could be corrected by i % 1 == 0 , but in this case the condition will always be true, because The remainder of the division by 1 is always 0.

As a result, we get either always a lie if we do not correct the second condition; either always the truth, if we change the division by taking the remainder in the second condition.

A prime number is a natural (positive integer) number that has exactly two different natural dividers - a unit and itself.

It is necessary to understand, check that the number is divided without remainder, and 1 will pass absolutely any number, not even an integer. And from the definition it follows that there are no other numbers on which it is possible to divide the number without a remainder and that is what needs to be checked.

PS: It is a pity that we have to explain this rather simple fact, instead of your math teacher, and it’s far from a fact that the teacher is to blame. And consider. that without a good knowledge of mathematics in programming, there is generally nothing to do, so restore gaps in mathematics before it is too late.

    I don’t know if there are any tags in C # to exit the loop, but here’s a solution to Java in the forehead:

     public static void main( String[] args ) { int res = 0; mark1: for ( int i = 2; i <= 1000; i++ ) { // for ( int j = i - 1; j != 1; j-- ) { for ( int j = (int)Math.sqrt( i ); j != 1; j-- ) { if ( i % j == 0 ) { continue mark1; } } res++; } System.out.println( res ); } 
    • There is a goto in SharPik. - Alexander Petrov