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

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

シクシク素数列 - C言語編

More than 1 year has passed since last update.

ルール

数値に4か9を含む素数をシクシク素数と呼ぶことにします
19とか41とか149とか。
標準入力として正の整数 N を与えたら N 番目までのシクシク素数を半角カンマ区切りで標準出力してください
例 N = 9 の場合、 19,29,41,43,47,59,79,89,97
N は最大で 100 とします

ソースコード

sksk.c
#include <stdio.h>

//数値から4と9を見つける
int find49(int i49_) {
    while (i49_ != 0)
        switch (i49_ % 10) {
        case 4:case 9: return 1;
        default: i49_ /= 10;
        }
    return 0;
}
//素数を探す
void prime(const int n_) {
    for (int i = 1, prime = 0, counter = 0;; ++i, prime = 0) {
        for (int j = 1; j <= i; ++j)
            if (i % j == 0) ++prime;
        if (prime == 2 && find49(i) == 1) {
            printf("%d", i);
            if (++counter != n_) printf(",");
            else break;
        }
    }
    printf("\n");
}
//入力値のシクシク素数を返す
int main() {
    int n;
    scanf_s("%d", &n);
    prime(n);
}

とりあえず30行で実装。
高速化はしてないです。

実行結果

出力結果(入力7)
19,29,41,43,47,59,79
出力結果(入力9)
19,29,41,43,47,59,79,89,97
出力結果(入力19)
19,29,41,43,47,59,79,89,97,109,139,149,179,191,193,197,199,229,239
出力結果(入力22)
19,29,41,43,47,59,79,89,97,109,139,149,179,191,193,197,199,229,239,241,269,293
出力結果(入力49)
19,29,41,43,47,59,79,89,97,109,139,149,179,191,193,197,199,229,239,241,269,293,347,349,359,379,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,509,541,547,569
出力結果(入力100)
19,29,41,43,47,59,79,89,97,109,139,149,179,191,193,197,199,229,239,241,269,293,347,349,359,379,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,509,541,547,569,593,599,619,641,643,647,659,691,709,719,739,743,769,797,809,829,839,859,907,911,919,929,937,941,947,953,967,971,977,983,991,997,1009,1019,1039,1049,1069,1091,1093,1097,1109,1129,1193,1229,1249,1259,1279,1289,1291,1297,1319

ソースコードのライセンス

These codes are licensed under CC0.
CC0

この記事のソースコードはCC0ライセンスとします。
ぜひ、自由に改変して遊んでみてください。

gis
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