Skip to content

Commit

Permalink
Add Problem Seven.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmarcoux111 committed Jan 10, 2015
1 parent 785d480 commit 7968fcf
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions ProblemSeven.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10 001st prime number?
*/
public class ProblemSeven {

public static void main(String[] args) {
int N = 1000000;

// initially assume all integers are prime
boolean[] isPrime = new boolean[N + 1];
for (int i = 2; i <= N; i++) {
isPrime[i] = true;
}

// mark non-primes <= N using Sieve of Erastothenes
for (int i = 2; i*i <= N; i++) {

// if i is prime, then mark multiples of i as nonprime
// suffices to consider mutiples i, i+1, ..., N/i
if (isPrime[i]) {
for (int j = i; i*j <= N; j++) {
isPrime[i*j] = false;
}
}
}

// count primes
int primes = 0;
outerloop:
for (int i = 2; i <= N; i++) {
if (isPrime[i]){ primes++;
if (primes==10001){
System.out.println(i);
break outerloop;
}
}
}
System.out.println("The number of primes <= " + N + " is " + primes);
}
}




0 comments on commit 7968fcf

Please sign in to comment.