Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PrimeArray {
- public static void main(String[] args) {
- long [] primes= new long [20];
- primes[0] = 2L;
- primes[1] = 3L;
- int count = 2;
- long number = 5L;
- outer:
- for(;count<primes.length;number +=2L) {
- long limit =(long)Math.ceil(Math.sqrt((double)number));
- for(int i=1;i<count && primes[i]<=limit;++i) {
- if(number%primes[i]==0L) {
- continue outer;
- }//fecha o condicional if
- }//fecha loop for i
- primes[count++] = number;
- }//fecha o loop for count
- for(long n:primes) {
- System.out.println(n);
- }
- }//fecha o void main
- }//fecha a classe PrimeArray
- Explicaçao do codigo (em ingles):
- How It Works
- Any number that is not a prime must be a product of prime factors, so you only need to divide a prime number
- candidate by prime numbers that are less than or equal to the square root of the candidate to test for whether it
- is prime. This is fairly obvious if you think about it. For every factor a number has that is greater than the square
- root of the number, the result of division by this factor is another factor that is less than the square root. You
- perhaps can see this more easily with a specific example. The number 24 has a square root that is a bit less than 5.
- You can factorize it as 2 *12, 3 *8, 4 *6; then you come to cases where the first factor is greater than the square
- root so the second is less, 6 *4, 8 *3, and so on, and so you are repeating the pairs of factors you already have.
- You first declare the array primes to be of type long and define it as having 20 elements. You set the first two
- elements of the primes array to 2 and 3, respectively, to start the process off, as you use the primes you have in
- the array as divisors when testing a new candidate.
- The variable count is the total number of primes you have found, so this starts out as 2 because you have
- already stored 2 and 3 in the first two elements of the primes array. Note that because you use count as the for
- loop control variable, you omit the first expression between parentheses in the loop statement, as the initial
- value of count has already been set.
- You store the candidate to be tested 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. You use the third control expression between the for
- loop parentheses to increment number in steps of two because you don't want to check even numbers. The
- for loop ends when count is equal to the length of the array. You test the value in number in the inner for
- loop by dividing number by all of the prime numbers you have in the primes array that are less than, or equal
- to, the square root of the candidate. If you get an exact division, the value in number is not prime, so you go
- immediately to the next iteration of the outer loop via the continue statement.
- You calculate the limit for divisors you need to try with the following statement:
- long limit = (long)ceil(sqrt((double)number));
- The sqrt() method from the Math class produces the square root of number as a double value, so if number
- has the value 7, for example, a value of about 2.64575 is returned. This is passed to the ceil() method, which
- 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 returns 3.0, the smallest
- integral value not less than the square root of 7. You want to use this number as the limit for your integer
- divisors, so you cast it to type long and store the result in limit. You are able to call the sqrt() and ceil()
- methods without qualifying their names with the class to which they belong because you have imported their
- names into the source file.
- The cast of number to type double is not strictly necessary. You could write the statement as:
- long limit = (long)ceil(sqrt(number));
- The compiler will insert the cast for you. However, by putting the explicit cast in, you indicate that it was your
- intention.
- If you don't get an exact division, you exit normally from the inner loop and execute the statement:
- primes[count++] = number; // We got one!
- Because count is the number of values you have stored, it also corresponds to the index for the next free
- element in the primes array. Thus, you use count as the index to the array element in which you want to store
- the value of number and then increment count.
- When you have filled the primes array, the outer loop ends and you output all the values in the array in the loop:
- for(long n : primes) {
- System.out.println(n); // Output all the primes
Advertisement
Add Comment
Please, Sign In to add comment