回答編集履歴
2
追加
test
CHANGED
@@ -21,3 +21,77 @@
|
|
21
21
|
prime_structure = (int *)calloc(result_int,sizeof(int));
|
22
22
|
|
23
23
|
```
|
24
|
+
|
25
|
+
エラトステネスの篩に興味が涌き、参考に部分作成してみました
|
26
|
+
|
27
|
+
```C
|
28
|
+
|
29
|
+
#include <stdio.h>
|
30
|
+
|
31
|
+
#include <math.h>
|
32
|
+
|
33
|
+
#include <stdlib.h>
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
int main(void){
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
double a_double = 9;
|
42
|
+
|
43
|
+
double b_double = 2;
|
44
|
+
|
45
|
+
unsigned int a_int,b_int,result_int;
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
result_int = pow(a_double,b_double);
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
a_int = a_double;
|
54
|
+
|
55
|
+
b_int = b_double;
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
//配列prime_structureの定義
|
60
|
+
|
61
|
+
unsigned int * prime_structure;
|
62
|
+
|
63
|
+
prime_structure = (int *)calloc(result_int-1,sizeof(int));
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
unsigned int i,j;
|
68
|
+
|
69
|
+
//エラトステネスのふるい
|
70
|
+
|
71
|
+
for (i = 2; i < result_int/2 ; i++) {
|
72
|
+
|
73
|
+
if (prime_structure[i] == 0){// prime[i]が素数なら
|
74
|
+
|
75
|
+
for (j = 2; i * j <= result_int-1; j++){
|
76
|
+
|
77
|
+
prime_structure[i * j] = 1; // 素数の倍数は素数ではない
|
78
|
+
|
79
|
+
}
|
80
|
+
|
81
|
+
}
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
for ( i = 1; i < result_int; i++){
|
86
|
+
|
87
|
+
if(prime_structure[ i ] == 0){
|
88
|
+
|
89
|
+
printf("%dは、素数\n",i);
|
90
|
+
|
91
|
+
}
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
```
|
1
訂正
test
CHANGED
@@ -18,6 +18,6 @@
|
|
18
18
|
|
19
19
|
unsigned int * prime_structure;
|
20
20
|
|
21
|
-
prime_structure = (int *)calloc(result_int
|
21
|
+
prime_structure = (int *)calloc(result_int,sizeof(int));
|
22
22
|
|
23
23
|
```
|