1. 100以内的质数有什么?

2 3 5 7
11 13 17 19 
23 29
31 37 
41 43 47 
53 59
61 67
71 73 79
83 89
97

2. 质数的定义

除了1 和 本身 不能被其他正整数整除的正整数。

3. 如何判断一个数是不是质数?

试除法。 如果x能被 2~x-1 中的一个数整除,则x不是质数。

#include <bits/stdc++.h>
using namespace std;
bool f(int x) {
	if (x<=1) return false;
	for (int i=2; i<=x-1; i++)
		if (x%i==0)
			return false;
	return true;	
}
int main() {
	if (f(14)==true) cout<<"14 is a prime."<<endl;	
	return 0;
}

如果x是质数,我们需要试除2~~~x-1,才行,需要试除的数很多。

我们知道,假如x是合数,那么x=ab,其中2<=a<=b, 则x=ab>=aa ,所以 axa \le \sqrt{x}

所以只需要试除到 x\sqrt{x}

#include <bits/stdc++.h>
using namespace std;
bool f(int x) {
	if (x<=1) return false;
	for (int i=2; i*i<=x; i++)
		if (x%i==0)
			return false;
	return true;	
}
int main() {
	if (f(14)==true) cout<<"14 is a prime."<<endl;	
	return 0;
}