Divisors of n | C ++ program to count number of divisors of n ^ 2 which are not divisible by divisor of n ; Function to count divisors of n ^ 2 having no factors of ' n ' ; Increment count of i - th prime divisor ; Find next prime divisor ; Increment count if divisor still remains ; Initialize variable for counting the factors of n ^ 2 and n as ans1 and ans2 respectively ; Range based for - loop ; Use formula as discussed in above ; return the difference of answers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factors ( long long n ) { unordered_map < int , int > prime ; for ( int i = 2 ; i <= sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { ++ prime [ i ] ; n = n / i ; } } if ( n > 2 ) ++ prime [ n ] ; int ans1 = 1 , ans2 = 1 ; for ( auto it : prime ) { ans1 *= 2 * it . second + 1 ; ans2 *= it . second + 1 ; } return ans1 - ans2 ; } int main ( ) { long long n = 5 ; cout << factors ( n ) << endl ; n = 8 ; cout << factors ( n ) ; return 0 ; }