Orbit counting theorem or Burnside 's Lemma | Utility function ; Function to find result using Orbit counting theorem or Burnside 's Lemma ; According to Burnside 's Lemma  calculate distinct ways for each  rotation ; Find GCD ; Divide By N ; Prlet the distinct ways ; N stones and M colors ; Function call
function gcd ( a , b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } function countDistinctWays ( N , M ) { let ans = 0 ; for ( let i = 0 ; i < N ; i ++ ) { let K = gcd ( i , N ) ; ans += Math . pow ( M , K ) ; } ans /= N ; document . write ( ans ) ; } let N = 4 , M = 3 ; countDistinctWays ( N , M ) ;