Cycle: for (j = 2; j < i; j++) , what is j<i . How to understand this? Where did we come up with j ?

 public class Za { public static void main(String args[]) { int i, j; // Объявление переменных i и j. boolean isprime; // Объявление переменной isprime for (i = 2; i < 100; i++) { // Объявление цикла isprime = true; //Вопрос: Зачем здесь это? for (j = 2; j < i; j++) /* Не ясно объявление цикла? Ведь i=2, а тут опять пишем j=2 и еще пишем j<i (как это возможно если Они оба равны 2). Вообще зачем, что написали внутри цикла */ if ((i % j) == 0) isprime = false; // Непонятно, почему мы делим по модулю и // если без остатка вернется false if (isprime) // Не понятно эта управляющая конструкция System.out.println(i + " is prime."); } } } // Почему создается это: isprime = true; // Проверить, делится ли число без остатка. for (j = 2; j < i; j++) // Если число делится без остатка, оно простое if ((i % j) == 0) isprime = false; 
  • 2
    The title should write a short title, and the questions are already in the body of the question. - Unick
  • 2
    Have you taken the code somewhere and cannot understand what it is doing? - iksuy
  • code took in the internet - KR88OS

4 answers 4

I added explanations with comments in the code:

 for (i = 2; i < 100; i++) { // перебираем все числа от двух до ста isprime = true; // если не доказано обратное - число простое for (j = 2; j < i; j++) // перебираем все числа от двух до текущего числа (хотя достаточно до Sqrt(i)) if ((i % j) == 0) isprime = false; // если остаток равен нулю, то текущее число i делится нацело на делитель j и простым быть не может. После этого должен стоять break, т.к. дальше искать делители бессмісленно if (isprime) // если не было найдено ни одного делителя, то число простое System.out.println(i + " is prime."); } 
  • isprime = true; // if the opposite is not proved - the number is simple, but why do we write this after for (i = 2; i <100; i ++) and not before - KR88OS
  • Because we need to check each of the numbers 2-100 for simplicity - Anton Shchyrov
  • I need a tutor upholstered free of charge urgently ( - KR88OS

This is an algorithm to search for primes up to 100, the second loop is all numbers less than the i-th to check (therefore, j <i), and isprime is a regular flag, which is finally checked to display the number in the console.

    // and it is not clear why we did that? because i = 2 and here we write again j = 2

    You have i=2 only in the first iteration of the cycle in i, in all subsequent ones there is no.

    // and it is not clear why we divide by module and say if without a trace the error (false)

    Again it is wrong, if it divides without remainder, then we assign a false value to the Boolean variable, and not an error. But in general:

    Homework must be done independently. If you have a question about homework, do not ask him to do it for you. Ask a specific question about a problem that you cannot solve. Programming is something that you need to understand yourself, or not to do it at all. If programming for you is a superfluous subject in the curriculum, there are sites and people on these sites that perform tasks for material rewards. Here to offer to do the work for you and vice versa - moveton.

      The point is that these are cycles, and you say that perform an internal cycle while j <i, as soon as this inequality stops, then exit the cycle. This makes sense since the next time i will be 3 4 5, etc. to 10