ProjectEuler - 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?

翻译:前6个质数是2,3,5,7,11,13, 第6个质数是13, 那么第10001个质数是多少?


解决方案:直接算到第10001个质数.


代码:

package projectEuler;

public class Probliem7 {
	private static int END = 10001;
	
	public static void main(String[] args){
		int count = 0;
		long i = 2;
		long result = 0;
		while(true){
			if(isPrime(i)){
				count++;
				result = i;
				if(count == END){
					break;
				}
			}
			i++;
		}
		System.out.printf("count=%d\n",count);
		System.out.printf("result=%d\n",result);
	}
	
	public static boolean isPrime(long num){
		for(long i=2; i<num; i++){
			if( 0 == num%i){
				return false;
			}
		}
		return true;
	}
}



你可能感兴趣的:(算法,欧拉项目)