이 튜토리얼에서는 숫자의 모든 완전약수의 개수를 찾는 프로그램에 대해 논의할 것입니다.
이를 위해 번호가 제공됩니다. 우리의 임무는 주어진 숫자의 모든 완벽한 약수를 세는 것입니다.
예시
#include<bits/stdc++.h>
using namespace std;
//checking perfect square
bool if_psquare(int n){
int sq = (int) sqrt(n);
return (n == sq * sq);
}
//returning count of perfect divisors
int count_pdivisors(int n){
int count = 0;
for (int i=1; i*i <= n; ++i){
if (n%i == 0){
if (if_psquare(i))
++count;
if (n/i != i && if_psquare(n/i))
++count;
}
}
return count;
}
int main(){
int n = 16;
cout << "Total perfect divisors of " << n << " = " << count_pdivisors(n) << "\n";
n = 12;
cout << "Total perfect divisors of " << n << " = " << count_pdivisors(n);
return 0;
} 출력
Total perfect divisors of 16 = 3 Total perfect divisors of 12 = 2