Check all the possible numbers that can divide p
for ( $ i = 3 ; $ i <= ( int ) sqrt ( $ p ) ; $ i += 2 ) { $ count = 0 ; while ( $ p % $ i == 0 ) { $ count ++ ; $ p = ( int ) ( $ p / $ i ) ; } if ( $ count > 0 ) { $ gcd *= pow ( $ i , ( int ) ( $ count / $ n ) ) ; } }