// [名前] sosu.c
// [機能] 素数
// [コンパイル]
// gcc -o sosu sosu.c -lm
#include<stdio.h>
#include<math.h>
// 実際に割ってみて、割切れるかどうかで素数判定
// (d > 2 であること)
int sosuu_desuka(int d)
{
int waru, kokomade;
// 3以上かつ偶数か?
if ((3 <= d) && (d % 2 == 0)) {
return 0;
}
// 割る数の最大値
kokomade
= (int) sqrt((double) d
); // 実際に割ってみる(3以上の奇数のみで)
for (waru = 3; waru <= kokomade; waru += 2) {
if (d % waru == 0) {
// 割り切れたので素数ではありませんでした
return 0;
}
}
// 素数でした
return 1;
}
int main()
{
int i; // ループ用
int ch; // 動作モード
int max; // 最大値
// ch=='v'の時は、求める素数の最大値を意味する
// ch=='n'の時は、求める個数を意味する
int count; // 個数カウント用
// 入力
fprintf(stderr
, " [v]指定した値までの素数を求める\n"); fprintf(stderr
, " [n]指定した個数の素数を求める\n"); if (ch == 'v') {
fprintf(stderr
, "いくつまでの素数を求めますか?(2~) = "); if (max < 2) {
return 1;
}
} else if (ch == 'n') {
fprintf(stderr
, "いくつ素数を求めますか?(1~) = "); if (max < 1) {
return 1;
}
} else {
fprintf(stderr
, "v か n を指定してください\n"); return 1;
}
// 表示
if (ch == 'v') {
for (i = 3; i <= max; i += 2) {
if (sosuu_desuka(i)) {
}
}
} else {
count = 1;
for (i = 3;; i += 2) {
if (count >= max) {
break;
}
if (sosuu_desuka(i)) {
count++;
}
}
}
// 終了
return 0;
}
Ly8gW+WQjeWJjV0gc29zdS5jCi8vIFvmqZ/og71dIOe0oOaVsAovLyBb44Kz44Oz44OR44Kk44OrXQovLyAgICAgIGdjYyAtbyBzb3N1IHNvc3UuYyAtbG0KI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8bWF0aC5oPgoKLy8g5a6f6Zqb44Gr5Ymy44Gj44Gm44G/44Gm44CB5Ymy5YiH44KM44KL44GL44Gp44GG44GL44Gn57Sg5pWw5Yik5a6aCi8vICAoZCA+IDIg44Gn44GC44KL44GT44GoKQppbnQgc29zdXVfZGVzdWthKGludCBkKQp7CiAgICBpbnQgd2FydSwga29rb21hZGU7CgogICAgLy8gM+S7peS4iuOBi+OBpOWBtuaVsOOBi++8nwogICAgaWYgKCgzIDw9IGQpICYmIChkICUgMiA9PSAwKSkgewogICAgICAgIHJldHVybiAwOwogICAgfQogICAgLy8g5Ymy44KL5pWw44Gu5pyA5aSn5YCkCiAgICBrb2tvbWFkZSA9IChpbnQpIHNxcnQoKGRvdWJsZSkgZCk7CiAgICAvLyDlrp/pmpvjgavlibLjgaPjgabjgb/jgosoM+S7peS4iuOBruWlh+aVsOOBruOBv+OBpykKICAgIGZvciAod2FydSA9IDM7IHdhcnUgPD0ga29rb21hZGU7IHdhcnUgKz0gMikgewogICAgICAgIGlmIChkICUgd2FydSA9PSAwKSB7CiAgICAgICAgICAgIC8vIOWJsuOCiuWIh+OCjOOBn+OBruOBp+e0oOaVsOOBp+OBr+OBguOCiuOBvuOBm+OCk+OBp+OBl+OBnwogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CiAgICB9CiAgICAvLyDntKDmlbDjgafjgZfjgZ8KICAgIHJldHVybiAxOwp9CgppbnQgbWFpbigpCnsKICAgIGludCBpOyAgICAgICAgICAgICAgICAgICAgICAvLyDjg6vjg7zjg5fnlKgKICAgIGludCBjaDsgICAgICAgICAgICAgICAgICAgICAvLyDli5XkvZzjg6Ljg7zjg4kKICAgIGludCBtYXg7ICAgICAgICAgICAgICAgICAgICAvLyDmnIDlpKflgKQKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLyAgIGNoPT0ndifjga7mmYLjga/jgIHmsYLjgoHjgovntKDmlbDjga7mnIDlpKflgKTjgpLmhI/lkbPjgZnjgosKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAvLyAgIGNoPT0nbifjga7mmYLjga/jgIHmsYLjgoHjgovlgIvmlbDjgpLmhI/lkbPjgZnjgosKICAgIGludCBjb3VudDsgICAgICAgICAgICAgICAgICAvLyDlgIvmlbDjgqvjgqbjg7Pjg4jnlKgKCiAgICAvLyDlhaXlipsKICAgIGZwcmludGYoc3RkZXJyLCAi57Sg5pWw44KS5rGC44KB44G+44GZXG4iKTsKICAgIGZwcmludGYoc3RkZXJyLCAiICBbdl3mjIflrprjgZfjgZ/lgKTjgb7jgafjga7ntKDmlbDjgpLmsYLjgoHjgotcbiIpOwogICAgZnByaW50ZihzdGRlcnIsICIgIFtuXeaMh+WumuOBl+OBn+WAi+aVsOOBrue0oOaVsOOCkuaxguOCgeOCi1xuIik7CiAgICBjaCA9IGdldGNoYXIoKTsKICAgIGlmIChjaCA9PSAndicpIHsKICAgICAgICBmcHJpbnRmKHN0ZGVyciwgIuOBhOOBj+OBpOOBvuOBp+OBrue0oOaVsOOCkuaxguOCgeOBvuOBmeOBi++8nygy772eKSA9ICIpOwogICAgICAgIHNjYW5mKCIlZCIsICZtYXgpOwogICAgICAgIGlmIChtYXggPCAyKSB7CiAgICAgICAgICAgIGZwcmludGYoc3RkZXJyLCAiMuS7peS4iuOCkuaMh+WumuOBl+OBpuOBj+OBoOOBleOBhFxuIik7CiAgICAgICAgICAgIHJldHVybiAxOwogICAgICAgIH0KICAgIH0gZWxzZSBpZiAoY2ggPT0gJ24nKSB7CiAgICAgICAgZnByaW50ZihzdGRlcnIsICLjgYTjgY/jgaTntKDmlbDjgpLmsYLjgoHjgb7jgZnjgYvvvJ8oMe+9nikgPSAiKTsKICAgICAgICBzY2FuZigiJWQiLCAmbWF4KTsKICAgICAgICBpZiAobWF4IDwgMSkgewogICAgICAgICAgICBmcHJpbnRmKHN0ZGVyciwgIjHku6XkuIrjgpLmjIflrprjgZfjgabjgY/jgaDjgZXjgYRcbiIpOwogICAgICAgICAgICByZXR1cm4gMTsKICAgICAgICB9CiAgICB9IGVsc2UgewogICAgICAgIGZwcmludGYoc3RkZXJyLCAidiDjgYsgbiDjgpLmjIflrprjgZfjgabjgY/jgaDjgZXjgYRcbiIpOwogICAgICAgIHJldHVybiAxOwogICAgfQoKICAgIC8vIOihqOekugogICAgaWYgKGNoID09ICd2JykgewogICAgICAgIHByaW50ZigiJWTjgb7jgafjga7ntKDmlbAgPSIsIG1heCk7CiAgICAgICAgcHJpbnRmKCIgIDIiKTsKICAgICAgICBmb3IgKGkgPSAzOyBpIDw9IG1heDsgaSArPSAyKSB7CiAgICAgICAgICAgIGlmIChzb3N1dV9kZXN1a2EoaSkpIHsKICAgICAgICAgICAgICAgIHByaW50ZigiICVkIiwgaSk7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9IGVsc2UgewogICAgICAgIHByaW50Zigi57Sg5pWwJWTlgIsgPSIsIG1heCk7CiAgICAgICAgcHJpbnRmKCIgIDIiKTsKICAgICAgICBjb3VudCA9IDE7CiAgICAgICAgZm9yIChpID0gMzs7IGkgKz0gMikgewogICAgICAgICAgICBpZiAoY291bnQgPj0gbWF4KSB7CiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgfQogICAgICAgICAgICBpZiAoc29zdXVfZGVzdWthKGkpKSB7CiAgICAgICAgICAgICAgICBwcmludGYoIiAlZCIsIGkpOwogICAgICAgICAgICAgICAgY291bnQrKzsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHByaW50ZigiXG4iKTsKCiAgICAvLyDntYLkuoYKICAgIHJldHVybiAwOwp9