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
using System ; using System . Collections . Generic ; class GFG { static int factors ( int n ) { Dictionary < int , int > prime = new Dictionary < int , int > ( ) ; for ( int i = 2 ; i <= Math . Sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { if ( prime . ContainsKey ( i ) ) { prime [ i ] = prime [ i ] + 1 ; } else { prime . Add ( i , 1 ) ; } n = n / i ; } } if ( n > 2 ) { if ( prime . ContainsKey ( n ) ) { prime [ n ] = prime [ n ] + 1 ; } else { prime . Add ( n , 1 ) ; } } int ans1 = 1 , ans2 = 1 ; foreach ( KeyValuePair < int , int > it in prime ) { ans1 *= 2 * it . Value + 1 ; ans2 *= it . Value + 1 ; } return ans1 - ans2 ; } public static void Main ( String [ ] args ) { int n = 5 ; Console . WriteLine ( factors ( n ) ) ; n = 8 ; Console . WriteLine ( factors ( n ) ) ; } }