หาจำนวนคู่ (a,b) ที่ a
ข้อสังเกต: จำนวนที่ติดกัน (consecutive) เป็น co-prime เสมอ
จำนวนคู่สูงสุด = floor((R - L + 1) / 2)
โค้ดตัวอย่าง
#include <iostream>
using namespace std;
int countCoPrimePairs(int L, int R) {
return (R - L + 1) / 2;
}
int main() {
int L = 1, R = 6;
cout << "Co-prime pairs: " << countCoPrimePairs(L, R);
return 0;
}
ผลลัพธ์
Co-prime pairs: 3
(คู่คือ (1,2), (3,4), (5,6))