中級 0件解決済み 正答率0%

Truncatable Primes

List the primes up to a limit that stay prime as digits are removed from either end.

このチャレンジで学べること

  • Remove digits from either end of a number
  • Test every truncation
  • Collect results in order

問題

翻訳を表示

Take 3797. Removing digits from the left gives 797, 97 and 7; removing them from the right gives 379, 37 and 3. All of them are prime, and so is 3797. Call such a prime two-sided truncatable. One-digit primes have nothing to remove and do not count. List every two-sided truncatable prime from 10 to limit.

Implement truncatable_primes(limit).

Input

limit: The largest number to check.

Output

Return the primes in increasing order. Return an empty list if there are none.

制約

1 ≤ limit ≤ 100,000

例

例 1
入力:
limit = 40

期待される出力:
expected = [23, 37]
入力:
long long limit = 40;

期待される出力:
std::vector<long long> expected = {23, 37};
入力:
long limit = 40;

期待される出力:
long[] expected = new long[] {23, 37};

解説

23 (2, 3), 37 (3, 7) both work; 13 fails because 1 is not prime and 29 fails because 9 is not. [23, 37].

例 2
入力:
limit = 10

期待される出力:
expected = []
入力:
long long limit = 10;

期待される出力:
std::vector<long long> expected = {};
入力:
long limit = 10;

期待される出力:
long[] expected = new long[] {};

解説

Only one-digit primes are below 10 and they do not count: [].

解き方のヒント

まずは自分で考えてから、ヒントを 1 つずつ開いてみましょう。

ヒント 1

Removing from the right is division by 10.

ヒント 2

Removing from the left is taking the remainder modulo a power of 10.

ヒント 3

Every truncation down to a single digit must be prime, and so must the number.

目安の計算量

O(limit × d × √limit) time, O(answer) extra space

参考資料

この課題はCodeUtility用に調整されています。上記の説明と制約に従ってください。

ランキング

全体ランキング

コーダーごとのいちばん良い合格解答です。速く、メモリが少なく、評価が高い解答ほど上位になります。

まだ合格した解答はありません。解いて 1 位になりましょう。

他の人の解答から学ぶ

コミュニティの解答

まだ公開された解答がありません。この課題を解いて最初の解答を共有しましょう。