3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シクシク素数列 - C++編

Last updated at Posted at 2018-12-06

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

#ソースコード

sksk.cpp
#include <iostream>
#include <cstdio>
//数値から4と9を見つける
bool find49(std::int_fast32_t i49_) {
	while (i49_)
		switch (i49_ % 10) {
		case 4:case 9: return true;
		default: i49_ /= 10;
		}
	return false;
}
//素数を探す
void prime(const std::int_fast32_t n_) {
	for (std::int_fast32_t i{ 1 }, prime{}, counter{};; ++i, prime = 0) {
		for (std::int_fast32_t j{ 1 }; j <= i; ++j)
			if (i % j == 0) ++prime;
		if (prime == 2 && find49(i)) {
			std::cout << i;
			if (++counter != n_) std::cout << ',';
			else break;
		}
	}
	std::cout << std::endl;
}
//入力値のシクシク素数を返す
int main() {
	std::int_fast32_t n;
	std::cin >> 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ライセンスとします。
ぜひ、自由に改変して遊んでみてください。

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?