ปานกลาง 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 กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก