Advertisement
sindrijo

Untitled

Oct 27th, 2011
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. package problems;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class Problem_010c {
  6.  
  7.     /**
  8.      * HAHAHA WOW, THIS EXECUTES FAST! Damn... I really need to learn proper arrays! :D
  9.      *
  10.      * Kudos to dplass1 from America.
  11.      *
  12.      *
  13.      * So how does this work exactly?... Lets look at it, hard!
  14.      *
  15.      *
  16.      */
  17.    
  18.  
  19.         public static void main(String[] args) {
  20.            
  21.             int n = 800000000;                              // Create and initialize integer n, 2 million. This is the "range" to search in.
  22.            
  23.             boolean isPrime[] = new boolean[n+1];           // Create an array that holds n+1 boolean values.
  24.            
  25.             Arrays.fill(isPrime, true);                     // Set ALL n+1 values in the isPrime Array to: TRUE.
  26.             isPrime[2] = true;                              // Set value at index 2, to true.
  27.             isPrime[3] = true;                              // Set value at index 3, to true. This is needed because what follows, you will understand once you've read the rest.
  28.            
  29.            
  30.                                                             // From here on the code is setting some values to false, I think the thought is that once you have done this
  31.                                                             // only primes will remain, and adding them up is trivial.
  32.            
  33.            
  34.             for (int i = 4; i < n; i += 2) isPrime[i] = false;  // Set all indexes from 4, and then every 2 slots to false. (This is basically a sieve, I'm starting to see.)
  35.                                                                 // What this does is set all even indexes from 4 and up in the array to false. Because index 2 should be true.
  36.            
  37.          // This looks like the prime finding code, a sieve!
  38.            
  39.             for (int i = 3; i < n; i += 2) {                // For i = 3 to n (the test-range), incrementing by 2, do the following. i = (3,5,7,9,11,13,15,17,19,21,23..)
  40.                                                             // While there are some primes listed there, none of these are "actually" put to false here.
  41.                
  42.                 for (int j = i+i; j < n; j += i) {          // For all j = i+i up to n, incrementing by i.  When i = 3 then j =  (6,9,12,15,18,21,24,27...)
  43.                     isPrime[j] = false;                     //                                              When i = 5 then j =  (10,15,20,25,30,35,40,45...)
  44.                 }                                           //                                              When i = 7 then j =  (14,21,28,35,42,49,56,63...)
  45.                                                             // What this is doing is setting all values representing multiples of odd numbers in the array to false. Clever!
  46.             }                                               // This of course includes multiples of all possible primes in the range provided.
  47.            
  48.                                                             // When this has been done for all multiples of odd numbers, and all even numbers in the array only
  49.                                                             // values with an address that is prime should remain. (In other words have the value TRUE.)
  50.            
  51.            
  52.             long sum = 0;                                   // Create and initialize a counter variable to get our total sum with.
  53.            
  54.             for (int i = 2; i < n; ++i) {                   // For all i = 2 up to n, incrementing by 1
  55.                 if (isPrime[i]) sum += i;                   // If the value in the array at index number i is true, then add i to the counter, because the array is telling it
  56.                                                             // that its a prime... :D
  57.             }
  58.             System.out.println("The sum is: " +sum);                        // Print the sum.
  59.         }
  60.    
  61.  
  62. }
  63.  
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement