测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A41256. 筛选N以内的素数

填空题 较易

题目描述

筛选N以内的素数

题目描述

用简单素数筛选法求N以内的素数。

输入格式

N

输出格式

2~N的素数

样例输入

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

参考答案

#include <cstdio> #include <algorithm> #include <cmath> using namespace std; int main() { int n; scanf("%d", &n); for (int i = 2; i <= n; ++i) { bool isPrime = true; for (int j = 2; j * j <= i; ++j) { if (i % j == 0) { isPrime = false; } } if (isPrime) { printf("%d\n", i); } } return 0; }
上一题 下一题