Advertisement
sindrijo

ProEuler P_010d

Nov 6th, 2011
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.95 KB | None | 0 0
  1. package problems;
  2.  
  3. public class Problem_010d {
  4.    
  5.    
  6.     /*
  7.      * Wow, this one is also quite fast and it doesn't use an array at all! Very clever code from a guy called blub123... lol...
  8.      *
  9.      */
  10.  
  11.     // main
  12.     public static void main(String[] args) {
  13.  
  14.         sumPrimesUnder(20000000,1);              // Print the final sum.
  15.     }
  16.    
  17.    
  18.     public static long sumPrimesUnder (long input, int a){
  19.         long testrange = input;
  20.         long answer = primeAdder(testrange);
  21.        
  22.         if(a == 1){ // Switch to suppress message or not. Good if being used in another code.
  23.             System.out.print("The sum of all primes under " + testrange +" is: " + answer +".");
  24.         }
  25.        
  26.         return answer;
  27.     }
  28.    
  29.    
  30.    
  31.     public static long primeAdder(long testrange){  
  32.        
  33.         // This method sends numbers into the isPrime method to check if they are prime, add them to a counter variable if they are and then return the sum.
  34.        
  35.         long result = 0;  // Create a counter.
  36.  
  37.  
  38.         // algorithm
  39.         for (int number = 0; number < testrange; number++) {  // For all Number = 0, up to 1000000, incrementing by two.
  40.             if (isPrime(number)) {                          // Call the primechecking method on the number.
  41.                 result += number;                           // If the primechecking method returns true, then add the number to the counter variable "result".
  42.             }
  43.         }
  44.         return result;
  45.        
  46.     }
  47.    
  48.  
  49.     // functions
  50.     // checks if a number is a prime
  51.     public static boolean isPrime(int number) {                 // This is a boolean method, returns true or false.
  52.        
  53.         if (number < 2) {                                       // If the number is less than two, return false.
  54.             return false;
  55.         } else if (number == 2) {                               // If the number equals to, return true. Because 2 is prime, duh.
  56.             return true;
  57.         } else if (number % 2 == 0 || number % 3 == 0 || number % 5 == 0 || number % 7 == 0 || number % 11 == 0 || number % 13 == 0 || number % 5 == 17) { 
  58.             // If the number is divisible by 2,3 or 5, return false. (This makes checking if even numbers are prime fast.
  59.             return false;
  60.         } else {                                                // End of IF statement, start of ELSE.
  61.            
  62.             for (int divisor = 3; divisor <= Math.sqrt(number); divisor += 2) { // For divisor 3 up to and including the square-root of the number, increment by two and check following:
  63.                 if (number % divisor == 0) {                                    // If the number is divisble by the divisor we are checking, return false.
  64.                     return false;                               // I was always trying to implement the root limit myself, but I always did it wrong and go some weird results.
  65.                 }
  66.             }
  67.             return true;                                                        // If the flow of execution reaches here, all divisors up to the root of the number we are testing for
  68.                                                                                 // have been tried, and therefore the number must be a prime.
  69.         }
  70.     }
  71. }
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement