Todos os desafios
Médio 0 resolvidos 0% de aceitação

Truncatable Primes

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

O que você vai aprender

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

Problema

Ver tradução

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.

Restrições

1 ≤ limit ≤ 100,000

Exemplos

Exemplo 1
Entrada:
limit = 40

Saída esperada:
expected = [23, 37]
Entrada:
long long limit = 40;

Saída esperada:
std::vector<long long> expected = {23, 37};
Entrada:
long limit = 40;

Saída esperada:
long[] expected = new long[] {23, 37};

Explicação

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

Exemplo 2
Entrada:
limit = 10

Saída esperada:
expected = []
Entrada:
long long limit = 10;

Saída esperada:
std::vector<long long> expected = {};
Entrada:
long limit = 10;

Saída esperada:
long[] expected = new long[] {};

Explicação

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

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

Removing from the right is division by 10.

Dica 2

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

Dica 3

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

Complexidade esperada

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

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

A melhor solução aprovada de cada programador. As mais rápidas, leves e mais bem avaliadas ficam no topo.

Ainda não há soluções aprovadas. Resolva e fique com o primeiro lugar.

Aprenda com outros

Soluções da comunidade

Ainda não há soluções publicadas. Resolva este desafio para compartilhar a primeira.