工学1号馆

home

Project Euler 7–10001st prime

Wu Yudong    September 26, 2017     欧拉计划   592   

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?

前六个质数是2,3,5,7,11和13,其中第6个是13.

第10001个质数是多少?

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>

int prim(int n)
{
	int i;
	for (i = 2; i * i <= n; i++) {
		if (n % i == 0)
			return 0;
	}
	return 1;
}

void solve(int n)
{
	int i = 2;
	int count = 0;
	while (1) {
		if (prim(i)) {
			count++;
			if (count == n)
				break;
		}
		i++;
	}
	printf("%d\n", i);
}

int main()
{
	int n = 10001;
	solve(n);
	return 0;
}

 Answer:104743

Completed on Thu, 4 Apr 2013, 17:34

如果文章对您有帮助,欢迎点击下方按钮打赏作者

Comments

No comments yet.
To verify that you are human, please fill in "七"(required)