- 98.1k
- 17
- 219
- 419
I've used the pseudo code from the Wikipedia page for the Sieve of EratosthenesWikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n: if A[i] is true: for j = i2i^2, i2+ii^2+i, i2+2ii^2+2i, ..., not exceeding n : A[j] := false
I've used the pseudo code from the Wikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n: if A[i] is true: for j = i2, i2+i, i2+2i, ..., not exceeding n : A[j] := false
I've used the pseudo code from the Wikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n: if A[i] is true: for j = i^2, i^2+i, i^2+2i, ..., not exceeding n : A[j] := false
- 35.2k
- 13
- 134
- 238
Suggestions for improvement on Project Euler #7
Here is a solution for problem #7 on projecteulerProject Euler Problem #7.net
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?
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?
II've used the pseudo code from the wikipediaWikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n:
if A[i] is true:
for j = i2, i2+i, i2+2i, ..., not exceeding n :
A[j] := false
---------------------------------------------------------------------------------
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n: if A[i] is true: for j = i2, i2+i, i2+2i, ..., not exceeding n : A[j] := false
Suggestions for improvement
Here is a solution for problem #7 on projecteuler.net
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?
I used the pseudo code from the wikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n:
if A[i] is true:
for j = i2, i2+i, i2+2i, ..., not exceeding n :
A[j] := false
---------------------------------------------------------------------------------
Suggestions for improvement on Project Euler #7
Here is a solution for Project Euler Problem #7.
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?
I've used the pseudo code from the Wikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n, initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n: if A[i] is true: for j = i2, i2+i, i2+2i, ..., not exceeding n : A[j] := false
- 753
- 1
- 7
- 16
Suggestions for improvmentimprovement
Here is a solution for problem #7 on projecteuler.net
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?
I used the pseudo code from the wikipedia page for the Sieve of Eratosthenes.
Here is a solution for problem #7 on projecteuler.net
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?
I used the pseudo code from the wikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n,initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n:
if A[i] is true:
for j = i2, i2+i, i2+2i, ..., not exceeding n :
A[j] := false
------------------------------------------------------------------------------------------
Any suggestions would be much appreciated.
import java.util.*;
public class FindPrimesHashMap {
public static void getPrime(int x) {
int n = (x * ((int)Math.sqrt(x) / 10)) + x;
int nSqrt = (int)Math.floor(Math.sqrt(n));
int j = 0;
int k = 0;
Map<Integer, Boolean> primeMap = new HashMap<Integer, Boolean>();
Set<Map.Entry<Integer, Boolean>> primes = primeMap.entrySet();
ArrayList<Integer> primeArray = new ArrayList<Integer>();
for (int a = 2; a < n; a++) {
primeMap.put(a, true);
}
for (int i = 2; i <= nSqrt; i++) {
if (primeMap.get(i)) {
while (true) {
j = (i * i) + (k * i);
k++;
if (j > n) {
break;
}
primeMap.put(j, false);
}
j = 0;
k = 0;
}
}
for (Map.Entry<Integer, Boolean> entry : primes) {
if (entry.getValue()) {
primeArray.add(entry.getKey());
}
}
Collections.sort(primeArray);
System.out.println(primeArray.get(x - 1));
}
Suggestions for improvment
Here is a solution for problem #7 on projecteuler.net
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?
I used the pseudo code from the wikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n,initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n:
if A[i] is true:
for j = i2, i2+i, i2+2i, ..., not exceeding n :
A[j] := false
------------------------------------------------------------------------------------------
import java.util.*;
public class FindPrimesHashMap {
public static void getPrime(int x) {
int n = (x * ((int)Math.sqrt(x) / 10)) + x;
int nSqrt = (int)Math.floor(Math.sqrt(n));
int j = 0;
int k = 0;
Map<Integer, Boolean> primeMap = new HashMap<Integer, Boolean>();
Set<Map.Entry<Integer, Boolean>> primes = primeMap.entrySet();
ArrayList<Integer> primeArray = new ArrayList<Integer>();
for (int a = 2; a < n; a++) {
primeMap.put(a, true);
}
for (int i = 2; i <= nSqrt; i++) {
if (primeMap.get(i)) {
while (true) {
j = (i * i) + (k * i);
k++;
if (j > n) {
break;
}
primeMap.put(j, false);
}
j = 0;
k = 0;
}
}
for (Map.Entry<Integer, Boolean> entry : primes) {
if (entry.getValue()) {
primeArray.add(entry.getKey());
}
}
Collections.sort(primeArray);
System.out.println(primeArray.get(x - 1));
}
Suggestions for improvement
Here is a solution for problem #7 on projecteuler.net
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?
I used the pseudo code from the wikipedia page for the Sieve of Eratosthenes.
Input: an integer n > 1
Let A be an array of Boolean values, indexed by integers 2 to n,initially all set to true.
for i = 2, 3, 4, ..., not exceeding √n:
if A[i] is true:
for j = i2, i2+i, i2+2i, ..., not exceeding n :
A[j] := false
---------------------------------------------------------------------------------
Any suggestions would be much appreciated.
import java.util.*;
public class FindPrimesHashMap {
public static void getPrime(int x) {
int n = (x * ((int)Math.sqrt(x) / 10)) + x;
int nSqrt = (int)Math.floor(Math.sqrt(n));
int j = 0;
int k = 0;
Map<Integer, Boolean> primeMap = new HashMap<Integer, Boolean>();
Set<Map.Entry<Integer, Boolean>> primes = primeMap.entrySet();
ArrayList<Integer> primeArray = new ArrayList<Integer>();
for (int a = 2; a < n; a++) {
primeMap.put(a, true);
}
for (int i = 2; i <= nSqrt; i++) {
if (primeMap.get(i)) {
while (true) {
j = (i * i) + (k * i);
k++;
if (j > n) {
break;
}
primeMap.put(j, false);
}
j = 0;
k = 0;
}
}
for (Map.Entry<Integer, Boolean> entry : primes) {
if (entry.getValue()) {
primeArray.add(entry.getKey());
}
}
Collections.sort(primeArray);
System.out.println(primeArray.get(x - 1));
}