모든 챌린지
보통 0명 해결 정답률 0%

Circular Primes

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

배우게 될 내용

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

문제

번역본 보기

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.

제약 조건

1 ≤ limit ≤ 100,000

예시

예시 1
입력:
limit = 20

예상 출력:
expected = 7
입력:
long long limit = 20;

예상 출력:
long long expected = 7;
입력:
long limit = 20;

예상 출력:
long expected = 7;

설명

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

예시 2
입력:
limit = 1

예상 출력:
expected = 0
입력:
long long limit = 1;

예상 출력:
long long expected = 0;
입력:
long limit = 1;

예상 출력:
long expected = 0;

설명

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

접근 방법

먼저 스스로 풀어 본 뒤 힌트를 하나씩 열어 보세요.

힌트 1

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

힌트 2

Turning the number into a string makes rotation easy.

힌트 3

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

예상 복잡도

O(limit × d × √(10^d)) time with trial division, O(d) extra space

참고 자료

이 문제는 CodeUtility에 맞게 수정되었습니다. 위의 설명과 제한 조건을 따르세요.

코더마다 가장 좋은 통과 풀이입니다. 더 빠르고, 메모리를 덜 쓰고, 평가가 좋은 풀이가 위에 옵니다.

아직 통과한 풀이가 없습니다. 풀어서 1위를 차지하세요.

다른 사람의 풀이 보기

커뮤니티 솔루션

아직 공개된 풀이가 없습니다. 문제를 풀고 첫 풀이를 공유하세요.