teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

追加

2016/07/21 22:59

投稿

A.Ichi
A.Ichi

スコア4070

answer CHANGED
@@ -9,4 +9,41 @@
9
9
  //配列prime_structureの定義
10
10
  unsigned int * prime_structure;
11
11
  prime_structure = (int *)calloc(result_int,sizeof(int));
12
+ ```
13
+ エラトステネスの篩に興味が涌き、参考に部分作成してみました
14
+ ```C
15
+ #include <stdio.h>
16
+ #include <math.h>
17
+ #include <stdlib.h>
18
+
19
+ int main(void){
20
+
21
+ double a_double = 9;
22
+ double b_double = 2;
23
+ unsigned int a_int,b_int,result_int;
24
+
25
+ result_int = pow(a_double,b_double);
26
+
27
+ a_int = a_double;
28
+ b_int = b_double;
29
+
30
+ //配列prime_structureの定義
31
+ unsigned int * prime_structure;
32
+ prime_structure = (int *)calloc(result_int-1,sizeof(int));
33
+
34
+ unsigned int i,j;
35
+ //エラトステネスのふるい
36
+ for (i = 2; i < result_int/2 ; i++) {
37
+ if (prime_structure[i] == 0){// prime[i]が素数なら
38
+ for (j = 2; i * j <= result_int-1; j++){
39
+ prime_structure[i * j] = 1; // 素数の倍数は素数ではない
40
+ }
41
+ }
42
+ }
43
+ for ( i = 1; i < result_int; i++){
44
+ if(prime_structure[ i ] == 0){
45
+ printf("%dは、素数\n",i);
46
+ }
47
+ }
48
+ }
12
49
  ```

1

訂正

2016/07/21 22:59

投稿

A.Ichi
A.Ichi

スコア4070

answer CHANGED
@@ -8,5 +8,5 @@
8
8
  ```
9
9
  //配列prime_structureの定義
10
10
  unsigned int * prime_structure;
11
- prime_structure = (int *)calloc(result_int-1,sizeof(int));
11
+ prime_structure = (int *)calloc(result_int,sizeof(int));
12
12
  ```