DINO_CH

Untitled

Nov 30th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. public class PrimeArray {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. long [] primes= new long [20];
  6. primes[0] = 2L;
  7. primes[1] = 3L;
  8. int count = 2;
  9.  
  10. long number = 5L;
  11.  
  12. outer:
  13. for(;count<primes.length;number +=2L) {
  14.  
  15. long limit =(long)Math.ceil(Math.sqrt((double)number));
  16.  
  17. for(int i=1;i<count && primes[i]<=limit;++i) {
  18.  
  19. if(number%primes[i]==0L) {
  20. continue outer;
  21. }//fecha o condicional if
  22. }//fecha loop for i
  23. primes[count++] = number;
  24. }//fecha o loop for count
  25.  
  26. for(long n:primes) {
  27. System.out.println(n);
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
  34. }//fecha o void main
  35.  
  36. }//fecha a classe PrimeArray
  37.  
  38. Explicaçao do codigo (em ingles):
  39. How It Works
  40. Any number that is not a prime must be a product of prime factors, so you only need to divide a prime number
  41. candidate by prime numbers that are less than or equal to the square root of the candidate to test for whether it
  42. is prime. This is fairly obvious if you think about it. For every factor a number has that is greater than the square
  43. root of the number, the result of division by this factor is another factor that is less than the square root. You
  44. perhaps can see this more easily with a specific example. The number 24 has a square root that is a bit less than 5.
  45. 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
  46. 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.
  47. You first declare the array primes to be of type long and define it as having 20 elements. You set the first two
  48. elements of the primes array to 2 and 3, respectively, to start the process off, as you use the primes you have in
  49. the array as divisors when testing a new candidate.
  50. The variable count is the total number of primes you have found, so this starts out as 2 because you have
  51. already stored 2 and 3 in the first two elements of the primes array. Note that because you use count as the for
  52. loop control variable, you omit the first expression between parentheses in the loop statement, as the initial
  53. value of count has already been set.
  54. You store the candidate to be tested in number, with the first value set as 5. The for loop statement labeled
  55. outer is slightly unusual. First of all, the variable count that determines when the loop ends is not incremented
  56. in the for loop statement, but in the body of the loop. You use the third control expression between the for
  57. loop parentheses to increment number in steps of two because you don't want to check even numbers. The
  58. for loop ends when count is equal to the length of the array. You test the value in number in the inner for
  59. loop by dividing number by all of the prime numbers you have in the primes array that are less than, or equal
  60. to, the square root of the candidate. If you get an exact division, the value in number is not prime, so you go
  61. immediately to the next iteration of the outer loop via the continue statement.
  62.  
  63. You calculate the limit for divisors you need to try with the following statement:
  64. long limit = (long)ceil(sqrt((double)number));
  65.  
  66. The sqrt() method from the Math class produces the square root of number as a double value, so if number
  67. has the value 7, for example, a value of about 2.64575 is returned. This is passed to the ceil() method, which
  68. is also a member of the Math class. The ceil() method returns a value of type double that is the minimum
  69. whole number that is not less than the value passed to it. With number as 7, this returns 3.0, the smallest
  70. integral value not less than the square root of 7. You want to use this number as the limit for your integer
  71. divisors, so you cast it to type long and store the result in limit. You are able to call the sqrt() and ceil()
  72. methods without qualifying their names with the class to which they belong because you have imported their
  73. names into the source file.
  74.  
  75. The cast of number to type double is not strictly necessary. You could write the statement as:
  76. long limit = (long)ceil(sqrt(number));
  77. The compiler will insert the cast for you. However, by putting the explicit cast in, you indicate that it was your
  78. intention.
  79.  
  80. If you don't get an exact division, you exit normally from the inner loop and execute the statement:
  81. primes[count++] = number; // We got one!
  82. Because count is the number of values you have stored, it also corresponds to the index for the next free
  83. element in the primes array. Thus, you use count as the index to the array element in which you want to store
  84. the value of number and then increment count.
  85.  
  86. When you have filled the primes array, the outer loop ends and you output all the values in the array in the loop:
  87. for(long n : primes) {
  88. System.out.println(n); // Output all the primes
Advertisement
Add Comment
Please, Sign In to add comment