ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อค้นหาการนับจำนวนที่มีตัวหารจำนวนคี่ในช่วงที่กำหนด
สำหรับสิ่งนี้ เราจะได้รับขีดจำกัดบนและล่างของช่วง งานของเราคือการคำนวณและนับจำนวนค่าที่มีตัวหารจำนวนคี่
ตัวอย่าง
#include <bits/stdc++.h>
using namespace std;
//counting the number of values
//with odd number of divisors
int OddDivCount(int a, int b){
int res = 0;
for (int i = a; i <= b; ++i) {
int divCount = 0;
for (int j = 1; j <= i; ++j) {
if (i % j == 0) {
++divCount;
}
}
if (divCount % 2) {
++res;
}
}
return res;
}
int main(){
int a = 1, b = 10;
cout << OddDivCount(a, b) << endl;
return 0;
} ผลลัพธ์
3