Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package problems;
- public class Problem_010a {
- /**
- *Find the sum of all the primes below two million.
- */
- /* Pseudocode: To find the sum of all the primes below two million.
- *
- * 1. Start looking for primes, start with prime nr. 3 and increment by two. (Use while loop)
- * 1a. In the primetest, only test with previously found primes.
- * 1b. In the primetest, only test numbers that are smaller than the square root of the number.
- * 2. When a prime is found check if its smaller than two million.
- * 3. If it is smaller.
- * 3a. Store it in the array.
- * 3b. Add it to the SUM variable.
- *
- *
- * */
- public static void main(String[] args) {
- System.out.println("Declaring variables...");
- final long start = 3;
- final long end = 100000;
- int primeCounter = 1;
- int arrayChecks = 0;
- int trialDivChecks = 0;
- long prime = 0;
- long primeSum = 2;
- long testrange = 100000;
- long[] anArray; // declares an array of integers
- anArray = new long[100000];
- anArray[1] = 2;
- System.out.println("Starting computation...\n");
- System.out.println("Testing odd numbers less than: " +testrange);
- System.out.println("\nINFO: We have stored " +primeCounter +" primes, because we already know 2 is a prime. \n");
- // START TESTING
- for (long number = start; number <= end; number += 2) {
- if (number > testrange) { // Stop testing for primes when the prime candidate is over 50.
- break;
- }
- trialDivChecks -= trialDivChecks; // Reset test counters
- // System.out.println("For loop is now testing: " +number +" \n"); // What number are you testing?
- if( primeCounter % 1000 == 0){
- System.out.println("INFO: Stored " +primeCounter +" primes in our array to check. Sum is up to: " +primeSum +" and latest prime is: " +number);
- }
- // prime array starts over here if number is divisble by number in array
- for (long i = 1; i <= number; i +=2) { // Prime tester starts
- arrayChecks -= arrayChecks;
- // System.out.println("**Just before OUT label.**");
- out:
- for(int arrayIndex = 1; arrayIndex <= primeCounter; arrayIndex++){ // For loop containing an ArrayCheck and modulo check
- arrayChecks++;
- // System.out.println("Running IF statement on array...");
- // System.out.println("Checking if " +number +" is divisble by the stored prime " +anArray[arrayIndex] +".");
- if (number % anArray[arrayIndex] == 0){ // Is prime stored in the array at index J is a factor of the number?
- // System.out.println("Number divisble by previous prime!:" + anArray[arrayIndex]);
- // System.out.println("Number of checks in Array?: " + arrayChecks +"\n");
- break out; // Yes it is, so stop running the FOR loop.
- } else if ( arrayChecks == arrayIndex+1){
- break out;
- }
- // System.out.println(number + " is not divisble by " +anArray[arrayIndex] +".");
- // System.out.println("");
- if (arrayIndex == primeCounter && i == number ){
- // System.out.println("Starting trial division...");
- if (number % i == 0) { // Trial Division Check STARTS
- trialDivChecks ++;
- if (i == number) {
- // System.out.print(number + " is prime");
- primeCounter++;
- prime = number;
- anArray[primeCounter] = prime;
- // System.out.println(", stored! \n");
- primeSum += prime;
- // System.out.println("Number of primes found and stored so far: " +primeCounter);
- }
- // System.out.println("Number of checks by trial?: " + trialDivChecks +"\n");
- break out;
- }
- }
- } // PrimeyArray Check ENDS
- }
- }
- System.out.println("Last Prime is Nr. " + primeCounter + ": " + anArray[primeCounter]);
- System.out.println("Sum of primes: " + primeSum);
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement