Submission
Status:
(PPPP)(PPP)(TSS)
Subtask/Task Score:
{40/40}{30/30}{0/30}
Score: 70
User: porpeang
Problemset: Twin, Cousin, and Sexy Prime
Language: cpp
Time: 1.097 second
Submitted On: 2026-10-08 09:59:05
#include <bits/stdc++.h>
using namespace std;
// ฟังก์ชันเช็คว่าเป็นจำนวนเฉพาะหรือไม่ (ใช้เช็คตัวเลขทั้งในและนอกช่วงได้)
bool isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return false;
}
return true;
}
int main() {
int a = 0, b = 0;
cin >> a >> b;
vector<int> ve;
// 1. หาจำนวนเฉพาะทั้งหมดในช่วง [a, b]
for (int i = a; i <= b; i++) { // แก้เป็น i <= b
if (isPrime(i)) {
ve.push_back(i);
}
}
int tw = 0, cou = 0, se = 0;
// 2. วนลูปเช็คจำนวนเฉพาะ p แต่ละตัวใน ve
for (int p : ve) {
// เช็ค Twin Prime (ต่างกัน 2)
if (isPrime(p - 2) || isPrime(p + 2)) {
tw++;
}
// เช็ค Cousin Prime (ต่างกัน 4)
if (isPrime(p - 4) || isPrime(p + 4)) {
cou++;
}
// เช็ค Sexy Prime (ต่างกัน 6)
if (isPrime(p - 6) || isPrime(p + 6)) {
se++;
}
}
cout << tw << '\n';
cout << cou << '\n';
cout << se << '\n';
return 0;
}