質數 (Prime)

質數 (Prime)

質數 教學與筆記。

說明

列印 size 個質數。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <stdio.h>

int is_prime(int input){
int flag = 1; // It's prime
for(int i = 2; i*i <= input; i++) {
if(input % i == 0 ){
flag = 0; // It's not prime
return flag;
}
}
return flag;
}

void main() {
for(int n = 2; n <= 43; n++) {
printf("n = %d, %d\n", n, is_prime(n));
}
}

結果 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
n = 2, 1
n = 3, 1
n = 4, 0
n = 5, 1
n = 6, 0
n = 7, 1
n = 8, 0
n = 9, 0
n = 10, 0
n = 11, 1
n = 12, 0
n = 13, 1
n = 14, 0
n = 15, 0
n = 16, 0
n = 17, 1
n = 18, 0
n = 19, 1
n = 20, 0
n = 21, 0
n = 22, 0
n = 23, 1
n = 24, 0
n = 25, 0
n = 26, 0
n = 27, 0
n = 28, 0
n = 29, 1
n = 30, 0
n = 31, 1
n = 32, 0
n = 33, 0
n = 34, 0
n = 35, 0
n = 36, 0
n = 37, 1
n = 38, 0
n = 39, 0
n = 40, 0
n = 41, 1
n = 42, 0
n = 43, 1
Author

Meow Lucian

Posted on

2019-07-08

Updated on

2022-07-04

Licensed under

Comments