Java Reference
In-Depth Information
The candidate to be tested is stored in number , with the first value set as 5. The for loop statement
labeled outer is slightly unusual. First of all, the variable count that determines when the loop ends is
not incremented in the for loop statement, but in the body of the loop. We use the third expression
between the for loop parentheses to increment number in steps of two, since we don't want to check
even numbers. The for loop ends when count is equal to the length of the array. We test the value in
number in the inner for loop by dividing number by all of the prime numbers we have in the primes
array that are less than, or equal to, the square root of the candidate. If we get an exact division the
value in number is not prime, so we go immediately to the next iteration of the outer loop via the
continue statement.
We calculate the limit for divisors we need to try with the statement:
long limit = (long)Math.ceil(Math.sqrt((double)number));
The Math.sqrt() method produces the square root of number as a double value, so if number has
the value 7, for instance, a value of about 2.64575 will be returned. This is passed to the ceil()
method that is also a member of the Math class. The ceil() method returns a value of type double
that is the minimum whole number that is not less than the value passed to it. With number as 7, this
will return 3.0, the smallest integral value not less than the square root of 7. We want to use this number
as the limit for our integer divisors, so we cast it to type long and store the result in limit .
If we get no exact division, we exit normally from the inner loop and execute the statement:
primes[count++] = number; // We got one!
Because count is the number of values we have stored, it also corresponds to the index for the next
free element in the primes array. Thus we use count as the index to the array element in which we
want to store the value of number , and then increment count .
When we have filled the primes array, the outer loop will end and we will output all the values in the
array. Note that, because we have used the length member of the primes object whenever we need
the number of elements in the array, changing the number of elements in the definition of the array to
generate a larger or smaller number of primes is simple.
We can express the logical process of the program with an algorithm as follows:
1.
Take the number in question and determine its square root.
2.
Set the limit for divisors to be the smallest integer that is greater than this square root value.
3.
Test to see if the number can be divided exactly (without remainder) by any of the primes
already in the primes array that are less than the limit for divisors.
4.
If it can, discard the existing number and start a new iteration of the loop with the next
candidate number . If it can't, it is a prime, so enter the existing number in the first available
empty slot in the array and then move to the next iteration for a new candidate number .
5.
If the array of primes is full, do no more iterations, and print out all the prime number values in
the array.
Search WWH ::




Custom Search