Count all perfect divisors of a number | Below is C ++ code to count total perfect divisors ; Pre - compute counts of all perfect divisors of all numbers upto MAX . ; Iterate through all the multiples of i * i ; Increment all such multiples by 1 ; Returns count of perfect divisors of n . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100001 NEW_LINE int perfectDiv [ MAX ] ; void precomputeCounts ( ) { for ( int i = 1 ; i * i < MAX ; ++ i ) { for ( int j = i * i ; j < MAX ; j += i * i ) ++ perfectDiv [ j ] ; } } int countPerfectDivisors ( int n ) { return perfectDiv [ n ] ; } int main ( ) { precomputeCounts ( ) ; int n = 16 ; cout << " Total ▁ perfect ▁ divisors ▁ of ▁ " << n << " ▁ = ▁ " << countPerfectDivisors ( n ) << " STRNEWLINE " ; n = 12 ; cout << " Total ▁ perfect ▁ divisors ▁ of ▁ " << n << " ▁ = ▁ " << countPerfectDivisors ( n ) ; return 0 ; }