Product of factors of number | C ++ program to calculate product of factors of number ; Iterative Function to calculate ( x ^ y ) in O ( log y ) ; function to count the factors ; If factors are equal , count only once ; Otherwise count both ; Calculate product of factors ; If numFactor is odd return power ( n , numFactor / 2 ) * sqrt ( n ) ; Driver code
#include <bits/stdc++.h> NEW_LINE #define M  1000000007 NEW_LINE using namespace std ; long long power ( long long x , long long y ) { long long res = 1 ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % M ; y = ( y >> 1 ) % M ; x = ( x * x ) % M ; } return res ; } int countFactors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count ++ ; else count += 2 ; } } return count ; } long long multiplyFactors ( int n ) { int numFactor = countFactors ( n ) ; long long product = power ( n , numFactor / 2 ) ; if ( numFactor & 1 ) product = ( product * ( int ) sqrt ( n ) ) % M ; return product ; } int main ( ) { int n = 12 ; cout << multiplyFactors ( n ) << endl ; return 0 ; }