class Primes{ public static void main(String[] args){ int N = Integer/parseInt(args[0]); boolean[] a = new boolean[N]; for (int i = 2; i<N; i++) a[i] = true; for (int i = 2; i<N; i++) if (a[i] != false) for (int j = i; j*i < N; j++) a[i*j] = false; for (int i = 2; i < N; i++) if (i > N - 100) // Зачем это проверять? if (a[i]) Out.print(" " + i); Out.println(); } } 

I do not understand why in the last cycle with the output of numbers to do the restriction if (i > N - 100) ?

PS program description, which is given in the textbook: This program is designed to print all prime numbers less than the integer specified by the argument on the command line. For this, an array of logical values ​​is calculated, in which a[i] is true if i prime number, and false otherwise. First, all elements of the array are entered true to indicate that there are no known composite numbers yet. Then in the elements of the array, the indices of which are composite numbers, is entered false . If, after setting all the divisors of primes smaller than i to false , the element a[i] is still true , then its index is a prime number

    1 answer 1

    What will happen if, at the start of the program, a very large N is entered - a million, ten? There will be enough memory for an array, and the program will work pretty quickly. But wait half a day to display hundreds of thousands of lines - to anything. Therefore, prime numbers are derived only from the last hundred - for visual inspection. With small N, when the whole table of prime numbers can be seen on one screen, it does not interfere.

    • Is the display on the screen more ponderous procedure than the actual calculation itself? Why does the program work quickly, and at the output it will slow down? - Dmitry
    • 2
      @Dmitry yes, I / O is usually much slower than computing. Here are the estimated numbers for the I / O delay associated with people.eecs.berkeley.edu/~rcs/research/interactive_latency.html - jfs