1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include <stdio.h>
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <math.h>
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3 #include "SchedTask.h"
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include "Prime.h"
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 #include "Func.h"
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 SchedDefineTask1(Prime, prime);
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
8
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9 static int
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
10 prime(SchedTask *smanager, void *rbuf, void *wbuf)
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
11 {
|
1232
|
12 long start = (long)smanager->get_param(0); /* 素数判定の開始地点 */
|
|
13 long end = (long)smanager->get_param(1); /* 素数判定の終了地点 */
|
|
14 long range = end - start; /* 判定する範囲 */
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
15
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
16 /* 判定結果を収める配列を受け取る */
|
1232
|
17 bool *output = (bool*)smanager->get_output(wbuf, 0);
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
18
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
19 /* 初期化 */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
20 for (int i = 0; i < range; i++){
|
1232
|
21 output[i] = true;
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
22 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
23
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
24
|
1232
|
25
|
|
26 for (long i = start, index = 0; i < end; i++, index++) {
|
|
27 long limit = (long)sqrt((double) i); /* 割る数の最大値を求める */
|
|
28 for (long j = 2; j <= limit; j++) {
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
29 /* 割り切れた場合、0を代入し素数じゃないという判定を下す */
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
30 if (i % j == 0) {
|
1232
|
31 output[index] = false;
|
1171
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
32 break;
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
33 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 }
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 return 0;
|
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 }
|