Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?

# AWKで素数列挙(エラトステネスのふるい・疑似素数による試し割り)

More than 3 years have passed since last update.

``````BEGIN {
upper_bound = 100

for (i = 2; i <= upper_bound; i++) is_prime[i] = 1;

for (i = 2; i <= sqrt(upper_bound); i++) {
if (!is_prime[i]) continue;
for (j = i * 2; j <= upper_bound; j += i) is_prime[j] = 0;
}

for (i = 2; i <= upper_bound; i++) {
if (is_prime[i]) print(i);
}
}
``````

``````\$ awk -f eratosthenes.AWK
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
``````

``````BEGIN {
upper_bound = 100

for (i = 0; i <= upper_bound; i++) {
if (is_prime(i)) print(i);
}
}

function is_prime(x,     prime, step) {
if (x < 2) return 0;
if (x == 2 || x == 3 || x == 5) return 1;
if (x % 2 == 0 || x % 3 == 0 || x % 5 == 0) return 0;

prime = 7
step = 4
while (prime <= sqrt(x)) {
if (x % prime == 0) return 0;
prime += step
step = 6 - step
}

return 1
}
``````

これも実行結果は以下の通りになります。

``````\$ awk -f prime.AWK
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
``````

``````\$ ruby -e 'require "prime"; Prime.each(100){|x| puts x}'
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
``````

ちなみに次のようにしても素数列挙は可能です(´・ω・｀)

``````\$ seq 1 100 | xargs factor | awk 'NF==2{print \$2}'
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
``````
1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away
IT業界の片隅でひっそり生きるシステムエンジニアです(´・ω・｀)