Advertisement
sindrijo

ProEuler P_010a

Nov 6th, 2011
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.58 KB | None | 0 0
  1. package problems;
  2.  
  3. public class Problem_010b {
  4.  
  5.     /* Pseudocode: To find the sum of all the primes below two million.
  6.      *
  7.      *
  8.      *
  9.      *  The Prime Number Theorem says that the number of primes not exceeding x can be approximated by x/ln(x).
  10.      *  
  11.      *  So the number of primes not exceeding 2.000.000 is:
  12.      *  
  13.      *  2.000.000/ln(2.000.000) = 137849 (Roughly)
  14.      *  
  15.      *  This means that there are about 138 thousand give or take primes lower than 2 million.
  16.      *
  17.      *  I actually cheated a bit and searched for the biggest prime under 2 million using a method from problem 3 that finds the biggest prime factor of a number,
  18.      *  if the biggest prime factor is the number you put in the number is a prime.
  19.      *  
  20.      *  The biggest prime under 2million is 1999993.
  21.      *  
  22.      *  Actually I could probably use that to add upp all the primes under 2 million by working my way down and checking on the way... maybe?
  23.      *
  24.      * Here is some more information from wikipedia:
  25.      *
  26.      * The simplest primality test is as follows:
  27.      *
  28.      * Given an input number n, check whether any integer m from 2 to n - 1 divides n.
  29.      * If n is divisible by any m then n is composite, otherwise it is prime.
  30.      *
  31.      * However, rather than testing all m up to n - 1, it is only necessary to test m up to sqrt(n): if n is composite then it can be factored into two values,
  32.      * at least one of which must be less than or equal to sqrt (n).
  33.      *
  34.      * The efficiency can also be improved by skipping all even m except 2, since if any even number divides n then 2 does.
  35.      *
  36.      *
  37.      * It can be improved further by observing that all primes are of the form 6k ± 1, with 2 and 3 being the only exceptions.
  38.      * This is because all integers can be expressed as (6k + i) for some integer k and for i = -1, 0, 1, 2, 3, or 4;
  39.      * 2 divides (6k + 0), (6k + 2), (6k + 4); and 3 divides (6k + 3).
  40.      *
  41.      * So a more efficient method is to test if n is divisible by 2 or 3, then to check through all the numbers of form 6k ± 1 =< sqrt (n).
  42.      * This is 3 times as fast as testing all m.
  43.      *
  44.      *
  45.      *
  46.      * Pseudocode Version 0.1
  47.      *
  48.      * 1. Start looking for primes, start with prime nr. 3 and increment by two. (Use while loop maybe?)
  49.      *  1a. In the primetest, only test numbers that are smaller than the square root of the number.
  50.      *  1b. In the primetest, only test with previously found primes.
  51.      *  
  52.      * 2. When a prime is found check if its smaller than two million.
  53.      *  If smaller:
  54.      *  3a. Store it in the array.
  55.      *  3b. Add it to the SUM variable.
  56.      *  If larger:
  57.      *   Stop the search/program.
  58.      *   Print the SUM.
  59.      *  
  60.      *   What variables do I need?
  61.      *  
  62.      *   First off all I need a counter to get the sum of the primes.
  63.      *  
  64.      *   long sumPrimes = 0;
  65.      *  
  66.      *   Then I need some kind of variable to hold the number I am currently testing.
  67.      *  
  68.      *   long testNumber = 0;
  69.      *  
  70.      *   I am only interested in testing numbers less than 2000000, so I can set a range.
  71.      *  
  72.      *   testRange = 2000000
  73.      *  
  74.      *
  75.      *
  76.      * */
  77.    
  78.     public static void primeFinder(){
  79.        
  80.    
  81.         int testRange = 2000000;
  82.         long primeSum = 2;
  83.         long lastPrimeSum = 2;
  84.         long currentPrime = 0;
  85.         int primesFound = 0;
  86.        
  87.         for( int i = testRange-1; 0 < i ; i -= 2 ){
  88.             tryagain:
  89.             if( i % 2 == 0 || i % 3 == 0 || i % 5 == 0 || i % 7 == 0 || i % 11 == 0 || i % 13 == 0 || i % 17 == 0 || i % 19 == 0){
  90.                 break tryagain;
  91.             }
  92.             lastPrimeSum = primeSum;
  93.             currentPrime = biggestFactor(i);
  94.             primeSum += currentPrime;
  95.            
  96.             if(lastPrimeSum < primeSum){
  97.                 primesFound++;
  98.                 if(primesFound % 1000 == 0){
  99.                 System.out.println(primesFound +" primes found, latest prime: " +currentPrime);
  100.                 }
  101.             }
  102.            
  103.         }
  104.         System.out.print("Final sum:" +primeSum);
  105.        
  106.        
  107.  
  108.        
  109.     }
  110.    
  111.    
  112.     public static long biggestFactor(long x)  // Returns the biggest prime factor of input.
  113.     {
  114.        long a = 3;
  115.        long input = x;
  116.  
  117.        
  118.        while ( x > 1 )
  119.        {           
  120.            if ( ( x % a ) == 0 )
  121.            {
  122.                x = x / a;
  123.                                                 // System.out.println("a = " +a); System.out.println("x = " +x);
  124.            }
  125.            else
  126.            {
  127.               a += 2;
  128.            }
  129.        }
  130.  
  131.        if( a == input){
  132.        return a;
  133.        } else {
  134.            return 0;
  135.        }
  136.     }
  137.    
  138.          
  139.     public static void main(String[] args) {
  140.         // TODO Auto-generated method stub
  141.        
  142.         primeFinder();
  143. //      System.out.println(biggestFactor(2));
  144.  
  145.  
  146.     }
  147.  
  148. }
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement