/ Published in: Java
Project Euler Question 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?"
Input: 10001
Output: 104743
Expand |
Embed | Plain Text
import java.math.*; public class Prob7{ int primenum = 0; long prime = 1; // I know 1 isn't a prime, but this makes the code easier to write while(primenum < nth){ prime = nextPrime(prime); primenum++; } } private static long nextPrime(long n){ n++; while(!isPrime(n)){ n++; } return n; } private static boolean isPrime(long n){ for(long i = 2; i <= limit; i++){ if(n % i == 0){ return false; } } return true; } }
You need to login to post a comment.
