Médio 0 resolvidos 0% de aceitação

Circular Primes

Count the primes up to a limit whose every digit rotation is also prime.

O que você vai aprender

  • Rotate the digits of a number
  • Check a condition on every rotation
  • Count numbers that pass

Problema

Ver tradução

Rotating a number moves its first digit to the end: 197 becomes 971, then 719. A prime is circular when every rotation is prime too, so 197 is circular. Count the circular primes from 2 to limit. A rotation may be larger than the limit; it still has to be prime.

Implement circular_primes(limit).

Input

limit: The largest number to check.

Output

Return how many primes p ≤ limit are circular.

Restrições

1 ≤ limit ≤ 100,000

Exemplos

Exemplo 1
Entrada:
limit = 20

Saída esperada:
expected = 7
Entrada:
long long limit = 20;

Saída esperada:
long long expected = 7;
Entrada:
long limit = 20;

Saída esperada:
long expected = 7;

Explicação

2, 3, 5, 7, 11, 13 (31 is prime) and 17 (71 is prime) are circular, but 19 is not (91 = 7 × 13): 7.

Exemplo 2
Entrada:
limit = 1

Saída esperada:
expected = 0
Entrada:
long long limit = 1;

Saída esperada:
long long expected = 0;
Entrada:
long limit = 1;

Saída esperada:
long expected = 0;

Explicação

There are no primes up to 1, so the answer is 0.

Como abordar

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

Dica 1

Rotating once moves the first digit to the end; a d-digit number has d rotations.

Dica 2

Turning the number into a string makes rotation easy.

Dica 3

Rotations can be bigger than the limit, so test them for primality directly.

Complexidade esperada

O(limit × d × √(10^d)) time with trial division, O(d) 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.