Tous les défis
Moyen 0 résolus 0 % de réussite

Truncatable Primes

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

Ce que vous allez apprendre

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

Problème

Afficher la traduction

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.

Contraintes

1 ≤ limit ≤ 100,000

Exemples

Exemple 1
Entrée :
limit = 40

Sortie attendue :
expected = [23, 37]
Entrée :
long long limit = 40;

Sortie attendue :
std::vector<long long> expected = {23, 37};
Entrée :
long limit = 40;

Sortie attendue :
long[] expected = new long[] {23, 37};

Explication

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

Exemple 2
Entrée :
limit = 10

Sortie attendue :
expected = []
Entrée :
long long limit = 10;

Sortie attendue :
std::vector<long long> expected = {};
Entrée :
long limit = 10;

Sortie attendue :
long[] expected = new long[] {};

Explication

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

Comment l'aborder

Essayez d'abord seul, puis ouvrez les indices un par un.

Indice 1

Removing from the right is division by 10.

Indice 2

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

Indice 3

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

Complexité attendue

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

Références

Ce défi a été adapté pour CodeUtility. Suivez l'énoncé et les contraintes ci-dessus.

La meilleure solution validée de chaque codeur. Les plus rapides, les plus légères et les mieux notées passent devant.

Aucune solution validée pour l'instant. Résolvez-le et prenez la première place.

Apprenez des autres

Solutions de la communauté

Aucune solution publiée pour l'instant. Résolvez ce défi pour partager la première.