Advertisement
sindrijo

ProEuler P_010a

Nov 6th, 2011
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. package problems;
  2.  
  3. public class Problem_010a {
  4.  
  5.     /**
  6.      *Find the sum of all the primes below two million.
  7.      */
  8.    
  9.     /* Pseudocode: To find the sum of all the primes below two million.
  10.      *
  11.      * 1. Start looking for primes, start with prime nr. 3 and increment by two. (Use while loop)
  12.      *  1a. In the primetest, only test with previously found primes.
  13.      *  1b. In the primetest, only test numbers that are smaller than the square root of the number.
  14.      * 2. When a prime is found check if its smaller than two million.
  15.      * 3. If it is smaller.
  16.      *  3a. Store it in the array.
  17.      *  3b. Add it to the SUM variable.
  18.      *
  19.      *
  20.      * */
  21.          
  22.     public static void main(String[] args) {
  23.                
  24.             System.out.println("Declaring variables...");
  25.                
  26.             final long start = 3;
  27.             final long end = 100000;
  28.            
  29.             int primeCounter = 1;
  30.            
  31.             int arrayChecks = 0;
  32.             int trialDivChecks = 0;
  33.            
  34.             long prime = 0;
  35.             long primeSum = 2;
  36.             long testrange = 100000;
  37.            
  38.             long[] anArray;              // declares an array of integers
  39.            
  40.  
  41.  
  42.             anArray = new long[100000];
  43.             anArray[1] = 2;
  44.  
  45.  
  46.  
  47.  
  48.            
  49.             System.out.println("Starting computation...\n");
  50.             System.out.println("Testing odd numbers less than: " +testrange);
  51.  
  52.             System.out.println("\nINFO: We have stored " +primeCounter +" primes, because we already know 2 is a prime. \n");
  53.  
  54.             // START TESTING
  55.                    
  56.                    
  57.             for (long number = start; number <= end; number += 2) {
  58.                
  59.                     if (number > testrange) {   // Stop testing for primes when the prime candidate is over 50.
  60.                             break;
  61.                     }
  62.  
  63.                    
  64.                    
  65.                     trialDivChecks -= trialDivChecks;   // Reset test counters
  66.  
  67.                    
  68.                    
  69.                  //   System.out.println("For loop is now testing: " +number +" \n");  // What number are you testing?
  70.                     if( primeCounter % 1000 == 0){
  71.                     System.out.println("INFO: Stored " +primeCounter +" primes in our array to check. Sum is up to: " +primeSum +" and latest prime is: " +number);
  72.                     }
  73.                      // prime array starts over here if number is divisble by number in array
  74.                    
  75.                     for (long i = 1; i <= number; i +=2) {  // Prime tester starts
  76.                            
  77.                         arrayChecks -= arrayChecks;
  78.                  //     System.out.println("**Just before OUT label.**");
  79.                         out:   
  80.                                
  81.                                 for(int arrayIndex = 1; arrayIndex <= primeCounter; arrayIndex++){  // For loop containing an ArrayCheck and modulo check
  82.                              
  83.                                     arrayChecks++;
  84.                                    
  85.  
  86.                                 //  System.out.println("Running IF statement on array...");
  87.                             //      System.out.println("Checking if " +number +" is divisble by the stored prime " +anArray[arrayIndex] +".");
  88.  
  89.                                    
  90.                                     if (number % anArray[arrayIndex] == 0){ // Is prime stored in the array at index J is a factor of the number?
  91.                                 //      System.out.println("Number divisble by previous prime!:" + anArray[arrayIndex]);
  92.                             //          System.out.println("Number of checks in Array?: " + arrayChecks +"\n");  
  93.                                         break out;                                                              // Yes it is, so stop running the FOR loop.
  94.                                     } else if ( arrayChecks == arrayIndex+1){
  95.                                         break out;
  96.                                     }
  97.                                 //  System.out.println(number + " is not divisble by " +anArray[arrayIndex] +".");
  98.                                    
  99.                                 //  System.out.println("");
  100.                                    
  101.                                    
  102.                                    
  103.                                     if (arrayIndex == primeCounter && i == number ){
  104.                                    
  105.                           //                  System.out.println("Starting trial division...");
  106.                                            
  107.                                             if (number % i == 0) {  // Trial Division Check STARTS
  108.                                                 trialDivChecks ++;
  109.                                                     if (i == number) {
  110.                                                        
  111.                                 //                          System.out.print(number + " is prime");
  112.                                                             primeCounter++;
  113.                                                             prime = number;
  114.                                                             anArray[primeCounter] = prime;
  115.                         //                                    System.out.println(", stored! \n");
  116.                                                             primeSum += prime;
  117.                                                            
  118.                         //                                    System.out.println("Number of primes found and stored so far: " +primeCounter);
  119.            
  120.                                                     }
  121.                       //                              System.out.println("Number of checks by trial?: " + trialDivChecks +"\n");
  122.                                                     break out;
  123.                                             }
  124.                                     }
  125.                                    
  126.                                    
  127.                                    
  128.                                    
  129.                                 } // PrimeyArray Check ENDS
  130.                            
  131.                            
  132.                                
  133.  
  134.                     }
  135.                    
  136.             }
  137.                                
  138.                                
  139.                     System.out.println("Last Prime is Nr. " + primeCounter + ": " + anArray[primeCounter]);
  140.                     System.out.println("Sum of primes: " + primeSum);
  141.    
  142.                     System.out.println();
  143.                            
  144.                            
  145.                     }
  146.    
  147.            
  148.     }
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement