Project Euler #7: 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?
How can I make this more efficient and cleaner?
public class Problem7 {
public static void main(String args) {
// a prime factor is something that can divide by only 1 and itself evenly
int counter = 0;
int primeNum = 0;
for (int num = 2; num < 10000000; num++) {
boolean isPrime = true;
for (int factor = 2; factor < num; factor++) {
if (num % factor == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeNum = num;
counter++;
}
if (counter == 10001) {
break;
}
}
System.out.println(primeNum);
}
}
java programming-challenge
add a comment |
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?
How can I make this more efficient and cleaner?
public class Problem7 {
public static void main(String args) {
// a prime factor is something that can divide by only 1 and itself evenly
int counter = 0;
int primeNum = 0;
for (int num = 2; num < 10000000; num++) {
boolean isPrime = true;
for (int factor = 2; factor < num; factor++) {
if (num % factor == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeNum = num;
counter++;
}
if (counter == 10001) {
break;
}
}
System.out.println(primeNum);
}
}
java programming-challenge
add a comment |
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?
How can I make this more efficient and cleaner?
public class Problem7 {
public static void main(String args) {
// a prime factor is something that can divide by only 1 and itself evenly
int counter = 0;
int primeNum = 0;
for (int num = 2; num < 10000000; num++) {
boolean isPrime = true;
for (int factor = 2; factor < num; factor++) {
if (num % factor == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeNum = num;
counter++;
}
if (counter == 10001) {
break;
}
}
System.out.println(primeNum);
}
}
java programming-challenge
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?
How can I make this more efficient and cleaner?
public class Problem7 {
public static void main(String args) {
// a prime factor is something that can divide by only 1 and itself evenly
int counter = 0;
int primeNum = 0;
for (int num = 2; num < 10000000; num++) {
boolean isPrime = true;
for (int factor = 2; factor < num; factor++) {
if (num % factor == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeNum = num;
counter++;
}
if (counter == 10001) {
break;
}
}
System.out.println(primeNum);
}
}
java programming-challenge
java programming-challenge
edited Jul 11 '16 at 9:26
BCdotWEB
8,63411638
8,63411638
asked Jul 9 '16 at 23:53
Ethan WatsonEthan Watson
8612
8612
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Magic numbers
You should use constants instead of some hard coded values. Adding a meaningful name to your constants improves the readability of the code.
Loop conditions
Instead of picking a huge value for the for loop, you have better alternatives for the end condition of the loop.
This option is similar and makes the intention of the code a little more clear:
for (int num = 2; num < Integer.MAX_VALUE; num++) {
You want to break the code when you find the n prime number, so you can use this in the condition:
for (int num = 2; counter < 10001; num++) {
To improve the performance, you can add a few changes to the code:
Don't test even numbers. All even numbers are divisible by 2.
for (int factor = 3; factor < num; factor = factor + 2) {
Don't test if the number is divisible by non-prime numbers. e.g. You don't need to test if a number is divisible by 6 (2x3), you already test with 2 and 3.
You can keep the prime numbers found so far and reuse them if you need to execute the code multiple times.
Making the code a little more generic, it could be:
public class PrimeFinder {
// Uses long instead of int to be able to find bigger primes
List<Long> primes = new ArrayList<>();
public static void main(String args) {
PrimeFinder finder = new PrimeFinder();
System.out.println(finder.getPrime(10001));
}
public PrimeFinder() {
// Start with 2 and 3 in the list to make it easy to iterate over odd numbers.
primes.add(2l);
primes.add(3l);
}
public long getPrime(int position) {
// If this was calculated previously, return the result.
if (position <= primes.size()) {
return primes.get(position - 1);
}
// Start the iteration after the last prime in the list. Skipping even numbers.
int count = primes.size();
for (long i = primes.get(primes.size() - 1) + 2; count < position; i = i + 2) {
if (isPrime(i)) {
count++;
}
}
return primes.get(primes.size() - 1);
}
private boolean isPrime(long number) {
for (long prime : primes) {
if (number % prime == 0) {
return false;
}
}
primes.add(number);
return true;
}
}
add a comment |
More efficient... look at Sieve of Eratosthenes for a huge speed up. I'm ignoring it for now, as it comes again and again.
for (int num = 2; num < 10000000; num++) {
The condition num < 10000000
is plain wrong. It works, but makes no sense. If the requirements change, the limit may be too small. Just drop it. Or better, replace it by counter < 10001
(nobody says, that you have to test num
, any condition is allowed).
You surely know that 4, 6, 8, ... are no primes, so you can skip them. Iterate over odd numbers only and set the initial value of counter equal to one (accounting for the skipped prime 2). So you get something like
for (int num = 3; counter < 10001; num += 2) {
This hardly helped the speed, but doing the same thing in
for (int factor = 2; factor < num; factor++) {
surely helps. You only have to deal with odd num
, so you test only odd factor
s.
Observe that for each factor
, also num / factor
is a factor and the smaller of the two is no bigger than the square root of num
. So you can use
int limit = (int) Math.sqrt(num);
for (int factor = 3; factor <= limit; factor += 2) {
if (isPrime) {
primeNum = num;
counter++;
}
This looks a bit strange. It works, but you assign primeNum
just in case it'll be needed. A rather useless variable as num
would do, too.
Even for such a rather trivial tasks you should use methods. Write small methods and let each of them do just one thing. Especially, never mix user input or output with computation as this makes the code perfectly non-reusable. Try to write methods like
boolean isPrime(int num);
int nextPrimeAbove(int num);
int nthPrime(int n);
add a comment |
package task;
import java.math.BigInteger;
/* 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 Prime {
public static int findPrime(int range) {
int i = 3;
int count = 1;
int res = 0;
while (count != range) {
BigInteger b = new BigInteger(i + "");
if (b.isProbablePrime(1)) {
count++;
res = i;
}
i += 2;
}
return res;
}
public static void main(String args) {
System.out.println(findPrime(10001));
}
}
/* Output : 104243 */
New contributor
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
4
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. Please read Why are alternative solutions not welcome?
– Sᴀᴍ Onᴇᴌᴀ
yesterday
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f134404%2fproject-euler-7-what-is-the-10-001st-prime-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Magic numbers
You should use constants instead of some hard coded values. Adding a meaningful name to your constants improves the readability of the code.
Loop conditions
Instead of picking a huge value for the for loop, you have better alternatives for the end condition of the loop.
This option is similar and makes the intention of the code a little more clear:
for (int num = 2; num < Integer.MAX_VALUE; num++) {
You want to break the code when you find the n prime number, so you can use this in the condition:
for (int num = 2; counter < 10001; num++) {
To improve the performance, you can add a few changes to the code:
Don't test even numbers. All even numbers are divisible by 2.
for (int factor = 3; factor < num; factor = factor + 2) {
Don't test if the number is divisible by non-prime numbers. e.g. You don't need to test if a number is divisible by 6 (2x3), you already test with 2 and 3.
You can keep the prime numbers found so far and reuse them if you need to execute the code multiple times.
Making the code a little more generic, it could be:
public class PrimeFinder {
// Uses long instead of int to be able to find bigger primes
List<Long> primes = new ArrayList<>();
public static void main(String args) {
PrimeFinder finder = new PrimeFinder();
System.out.println(finder.getPrime(10001));
}
public PrimeFinder() {
// Start with 2 and 3 in the list to make it easy to iterate over odd numbers.
primes.add(2l);
primes.add(3l);
}
public long getPrime(int position) {
// If this was calculated previously, return the result.
if (position <= primes.size()) {
return primes.get(position - 1);
}
// Start the iteration after the last prime in the list. Skipping even numbers.
int count = primes.size();
for (long i = primes.get(primes.size() - 1) + 2; count < position; i = i + 2) {
if (isPrime(i)) {
count++;
}
}
return primes.get(primes.size() - 1);
}
private boolean isPrime(long number) {
for (long prime : primes) {
if (number % prime == 0) {
return false;
}
}
primes.add(number);
return true;
}
}
add a comment |
Magic numbers
You should use constants instead of some hard coded values. Adding a meaningful name to your constants improves the readability of the code.
Loop conditions
Instead of picking a huge value for the for loop, you have better alternatives for the end condition of the loop.
This option is similar and makes the intention of the code a little more clear:
for (int num = 2; num < Integer.MAX_VALUE; num++) {
You want to break the code when you find the n prime number, so you can use this in the condition:
for (int num = 2; counter < 10001; num++) {
To improve the performance, you can add a few changes to the code:
Don't test even numbers. All even numbers are divisible by 2.
for (int factor = 3; factor < num; factor = factor + 2) {
Don't test if the number is divisible by non-prime numbers. e.g. You don't need to test if a number is divisible by 6 (2x3), you already test with 2 and 3.
You can keep the prime numbers found so far and reuse them if you need to execute the code multiple times.
Making the code a little more generic, it could be:
public class PrimeFinder {
// Uses long instead of int to be able to find bigger primes
List<Long> primes = new ArrayList<>();
public static void main(String args) {
PrimeFinder finder = new PrimeFinder();
System.out.println(finder.getPrime(10001));
}
public PrimeFinder() {
// Start with 2 and 3 in the list to make it easy to iterate over odd numbers.
primes.add(2l);
primes.add(3l);
}
public long getPrime(int position) {
// If this was calculated previously, return the result.
if (position <= primes.size()) {
return primes.get(position - 1);
}
// Start the iteration after the last prime in the list. Skipping even numbers.
int count = primes.size();
for (long i = primes.get(primes.size() - 1) + 2; count < position; i = i + 2) {
if (isPrime(i)) {
count++;
}
}
return primes.get(primes.size() - 1);
}
private boolean isPrime(long number) {
for (long prime : primes) {
if (number % prime == 0) {
return false;
}
}
primes.add(number);
return true;
}
}
add a comment |
Magic numbers
You should use constants instead of some hard coded values. Adding a meaningful name to your constants improves the readability of the code.
Loop conditions
Instead of picking a huge value for the for loop, you have better alternatives for the end condition of the loop.
This option is similar and makes the intention of the code a little more clear:
for (int num = 2; num < Integer.MAX_VALUE; num++) {
You want to break the code when you find the n prime number, so you can use this in the condition:
for (int num = 2; counter < 10001; num++) {
To improve the performance, you can add a few changes to the code:
Don't test even numbers. All even numbers are divisible by 2.
for (int factor = 3; factor < num; factor = factor + 2) {
Don't test if the number is divisible by non-prime numbers. e.g. You don't need to test if a number is divisible by 6 (2x3), you already test with 2 and 3.
You can keep the prime numbers found so far and reuse them if you need to execute the code multiple times.
Making the code a little more generic, it could be:
public class PrimeFinder {
// Uses long instead of int to be able to find bigger primes
List<Long> primes = new ArrayList<>();
public static void main(String args) {
PrimeFinder finder = new PrimeFinder();
System.out.println(finder.getPrime(10001));
}
public PrimeFinder() {
// Start with 2 and 3 in the list to make it easy to iterate over odd numbers.
primes.add(2l);
primes.add(3l);
}
public long getPrime(int position) {
// If this was calculated previously, return the result.
if (position <= primes.size()) {
return primes.get(position - 1);
}
// Start the iteration after the last prime in the list. Skipping even numbers.
int count = primes.size();
for (long i = primes.get(primes.size() - 1) + 2; count < position; i = i + 2) {
if (isPrime(i)) {
count++;
}
}
return primes.get(primes.size() - 1);
}
private boolean isPrime(long number) {
for (long prime : primes) {
if (number % prime == 0) {
return false;
}
}
primes.add(number);
return true;
}
}
Magic numbers
You should use constants instead of some hard coded values. Adding a meaningful name to your constants improves the readability of the code.
Loop conditions
Instead of picking a huge value for the for loop, you have better alternatives for the end condition of the loop.
This option is similar and makes the intention of the code a little more clear:
for (int num = 2; num < Integer.MAX_VALUE; num++) {
You want to break the code when you find the n prime number, so you can use this in the condition:
for (int num = 2; counter < 10001; num++) {
To improve the performance, you can add a few changes to the code:
Don't test even numbers. All even numbers are divisible by 2.
for (int factor = 3; factor < num; factor = factor + 2) {
Don't test if the number is divisible by non-prime numbers. e.g. You don't need to test if a number is divisible by 6 (2x3), you already test with 2 and 3.
You can keep the prime numbers found so far and reuse them if you need to execute the code multiple times.
Making the code a little more generic, it could be:
public class PrimeFinder {
// Uses long instead of int to be able to find bigger primes
List<Long> primes = new ArrayList<>();
public static void main(String args) {
PrimeFinder finder = new PrimeFinder();
System.out.println(finder.getPrime(10001));
}
public PrimeFinder() {
// Start with 2 and 3 in the list to make it easy to iterate over odd numbers.
primes.add(2l);
primes.add(3l);
}
public long getPrime(int position) {
// If this was calculated previously, return the result.
if (position <= primes.size()) {
return primes.get(position - 1);
}
// Start the iteration after the last prime in the list. Skipping even numbers.
int count = primes.size();
for (long i = primes.get(primes.size() - 1) + 2; count < position; i = i + 2) {
if (isPrime(i)) {
count++;
}
}
return primes.get(primes.size() - 1);
}
private boolean isPrime(long number) {
for (long prime : primes) {
if (number % prime == 0) {
return false;
}
}
primes.add(number);
return true;
}
}
edited Jul 11 '16 at 9:11
answered Jul 11 '16 at 9:01
David SNDavid SN
46127
46127
add a comment |
add a comment |
More efficient... look at Sieve of Eratosthenes for a huge speed up. I'm ignoring it for now, as it comes again and again.
for (int num = 2; num < 10000000; num++) {
The condition num < 10000000
is plain wrong. It works, but makes no sense. If the requirements change, the limit may be too small. Just drop it. Or better, replace it by counter < 10001
(nobody says, that you have to test num
, any condition is allowed).
You surely know that 4, 6, 8, ... are no primes, so you can skip them. Iterate over odd numbers only and set the initial value of counter equal to one (accounting for the skipped prime 2). So you get something like
for (int num = 3; counter < 10001; num += 2) {
This hardly helped the speed, but doing the same thing in
for (int factor = 2; factor < num; factor++) {
surely helps. You only have to deal with odd num
, so you test only odd factor
s.
Observe that for each factor
, also num / factor
is a factor and the smaller of the two is no bigger than the square root of num
. So you can use
int limit = (int) Math.sqrt(num);
for (int factor = 3; factor <= limit; factor += 2) {
if (isPrime) {
primeNum = num;
counter++;
}
This looks a bit strange. It works, but you assign primeNum
just in case it'll be needed. A rather useless variable as num
would do, too.
Even for such a rather trivial tasks you should use methods. Write small methods and let each of them do just one thing. Especially, never mix user input or output with computation as this makes the code perfectly non-reusable. Try to write methods like
boolean isPrime(int num);
int nextPrimeAbove(int num);
int nthPrime(int n);
add a comment |
More efficient... look at Sieve of Eratosthenes for a huge speed up. I'm ignoring it for now, as it comes again and again.
for (int num = 2; num < 10000000; num++) {
The condition num < 10000000
is plain wrong. It works, but makes no sense. If the requirements change, the limit may be too small. Just drop it. Or better, replace it by counter < 10001
(nobody says, that you have to test num
, any condition is allowed).
You surely know that 4, 6, 8, ... are no primes, so you can skip them. Iterate over odd numbers only and set the initial value of counter equal to one (accounting for the skipped prime 2). So you get something like
for (int num = 3; counter < 10001; num += 2) {
This hardly helped the speed, but doing the same thing in
for (int factor = 2; factor < num; factor++) {
surely helps. You only have to deal with odd num
, so you test only odd factor
s.
Observe that for each factor
, also num / factor
is a factor and the smaller of the two is no bigger than the square root of num
. So you can use
int limit = (int) Math.sqrt(num);
for (int factor = 3; factor <= limit; factor += 2) {
if (isPrime) {
primeNum = num;
counter++;
}
This looks a bit strange. It works, but you assign primeNum
just in case it'll be needed. A rather useless variable as num
would do, too.
Even for such a rather trivial tasks you should use methods. Write small methods and let each of them do just one thing. Especially, never mix user input or output with computation as this makes the code perfectly non-reusable. Try to write methods like
boolean isPrime(int num);
int nextPrimeAbove(int num);
int nthPrime(int n);
add a comment |
More efficient... look at Sieve of Eratosthenes for a huge speed up. I'm ignoring it for now, as it comes again and again.
for (int num = 2; num < 10000000; num++) {
The condition num < 10000000
is plain wrong. It works, but makes no sense. If the requirements change, the limit may be too small. Just drop it. Or better, replace it by counter < 10001
(nobody says, that you have to test num
, any condition is allowed).
You surely know that 4, 6, 8, ... are no primes, so you can skip them. Iterate over odd numbers only and set the initial value of counter equal to one (accounting for the skipped prime 2). So you get something like
for (int num = 3; counter < 10001; num += 2) {
This hardly helped the speed, but doing the same thing in
for (int factor = 2; factor < num; factor++) {
surely helps. You only have to deal with odd num
, so you test only odd factor
s.
Observe that for each factor
, also num / factor
is a factor and the smaller of the two is no bigger than the square root of num
. So you can use
int limit = (int) Math.sqrt(num);
for (int factor = 3; factor <= limit; factor += 2) {
if (isPrime) {
primeNum = num;
counter++;
}
This looks a bit strange. It works, but you assign primeNum
just in case it'll be needed. A rather useless variable as num
would do, too.
Even for such a rather trivial tasks you should use methods. Write small methods and let each of them do just one thing. Especially, never mix user input or output with computation as this makes the code perfectly non-reusable. Try to write methods like
boolean isPrime(int num);
int nextPrimeAbove(int num);
int nthPrime(int n);
More efficient... look at Sieve of Eratosthenes for a huge speed up. I'm ignoring it for now, as it comes again and again.
for (int num = 2; num < 10000000; num++) {
The condition num < 10000000
is plain wrong. It works, but makes no sense. If the requirements change, the limit may be too small. Just drop it. Or better, replace it by counter < 10001
(nobody says, that you have to test num
, any condition is allowed).
You surely know that 4, 6, 8, ... are no primes, so you can skip them. Iterate over odd numbers only and set the initial value of counter equal to one (accounting for the skipped prime 2). So you get something like
for (int num = 3; counter < 10001; num += 2) {
This hardly helped the speed, but doing the same thing in
for (int factor = 2; factor < num; factor++) {
surely helps. You only have to deal with odd num
, so you test only odd factor
s.
Observe that for each factor
, also num / factor
is a factor and the smaller of the two is no bigger than the square root of num
. So you can use
int limit = (int) Math.sqrt(num);
for (int factor = 3; factor <= limit; factor += 2) {
if (isPrime) {
primeNum = num;
counter++;
}
This looks a bit strange. It works, but you assign primeNum
just in case it'll be needed. A rather useless variable as num
would do, too.
Even for such a rather trivial tasks you should use methods. Write small methods and let each of them do just one thing. Especially, never mix user input or output with computation as this makes the code perfectly non-reusable. Try to write methods like
boolean isPrime(int num);
int nextPrimeAbove(int num);
int nthPrime(int n);
edited Jul 10 '16 at 1:12
mdfst13
17.4k52156
17.4k52156
answered Jul 10 '16 at 0:29
maaartinusmaaartinus
12.3k12669
12.3k12669
add a comment |
add a comment |
package task;
import java.math.BigInteger;
/* 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 Prime {
public static int findPrime(int range) {
int i = 3;
int count = 1;
int res = 0;
while (count != range) {
BigInteger b = new BigInteger(i + "");
if (b.isProbablePrime(1)) {
count++;
res = i;
}
i += 2;
}
return res;
}
public static void main(String args) {
System.out.println(findPrime(10001));
}
}
/* Output : 104243 */
New contributor
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
4
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. Please read Why are alternative solutions not welcome?
– Sᴀᴍ Onᴇᴌᴀ
yesterday
add a comment |
package task;
import java.math.BigInteger;
/* 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 Prime {
public static int findPrime(int range) {
int i = 3;
int count = 1;
int res = 0;
while (count != range) {
BigInteger b = new BigInteger(i + "");
if (b.isProbablePrime(1)) {
count++;
res = i;
}
i += 2;
}
return res;
}
public static void main(String args) {
System.out.println(findPrime(10001));
}
}
/* Output : 104243 */
New contributor
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
4
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. Please read Why are alternative solutions not welcome?
– Sᴀᴍ Onᴇᴌᴀ
yesterday
add a comment |
package task;
import java.math.BigInteger;
/* 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 Prime {
public static int findPrime(int range) {
int i = 3;
int count = 1;
int res = 0;
while (count != range) {
BigInteger b = new BigInteger(i + "");
if (b.isProbablePrime(1)) {
count++;
res = i;
}
i += 2;
}
return res;
}
public static void main(String args) {
System.out.println(findPrime(10001));
}
}
/* Output : 104243 */
New contributor
package task;
import java.math.BigInteger;
/* 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 Prime {
public static int findPrime(int range) {
int i = 3;
int count = 1;
int res = 0;
while (count != range) {
BigInteger b = new BigInteger(i + "");
if (b.isProbablePrime(1)) {
count++;
res = i;
}
i += 2;
}
return res;
}
public static void main(String args) {
System.out.println(findPrime(10001));
}
}
/* Output : 104243 */
New contributor
New contributor
answered yesterday
Sudhir PaleiSudhir Palei
1
1
New contributor
New contributor
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
We are looking for answers that provide insightful observations about the code in the question. Answers that consist of independent solutions with no justification do not constitute a code review, and may be removed.
4
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. Please read Why are alternative solutions not welcome?
– Sᴀᴍ Onᴇᴌᴀ
yesterday
add a comment |
4
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. Please read Why are alternative solutions not welcome?
– Sᴀᴍ Onᴇᴌᴀ
yesterday
4
4
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. Please read Why are alternative solutions not welcome?
– Sᴀᴍ Onᴇᴌᴀ
yesterday
Welcome to Code Review! You have presented an alternative solution, but haven't reviewed the code. Please explain your reasoning (how your solution works and why it is better than the original) so that the author and other readers can learn from your thought process. Please read Why are alternative solutions not welcome?
– Sᴀᴍ Onᴇᴌᴀ
yesterday
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f134404%2fproject-euler-7-what-is-the-10-001st-prime-number%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown