Tutte le sfide
Media 0 risolte 0% di accettazione

Truncatable Primes

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

Cosa imparerai

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

Problema

Mostra traduzione

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.

Vincoli

1 ≤ limit ≤ 100,000

Esempi

Esempio 1
Input:
limit = 40

Output atteso:
expected = [23, 37]
Input:
long long limit = 40;

Output atteso:
std::vector<long long> expected = {23, 37};
Input:
long limit = 40;

Output atteso:
long[] expected = new long[] {23, 37};

Spiegazione

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

Esempio 2
Input:
limit = 10

Output atteso:
expected = []
Input:
long long limit = 10;

Output atteso:
std::vector<long long> expected = {};
Input:
long limit = 10;

Output atteso:
long[] expected = new long[] {};

Spiegazione

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

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Removing from the right is division by 10.

Suggerimento 2

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

Suggerimento 3

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

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

Ancora nessuna soluzione superata. Risolvila e prendi il primo posto.

Impara dagli altri

Soluzioni della community

Nessuna soluzione pubblicata. Risolvi questa sfida per condividere la prima.