Game Theory in Balanced Ternary Numeral System | ( Moving 3 k steps at a time ) | Numbers are in range of pow ( 3 , 32 ) ; Conversion of ternary into balanced ternary as start iterating from Least Significant Bit ( i . e 0 th ) , if encountered 0 or 1 , safely skip and pass carry 0 further 2 , replace it to - 1 and pass carry 1 further 3 , replace it to 0 and pass carry 1 further ; Similar to binary conversion ; Driver code ; Moving on to first occupied bit ; Printing ; Print ' Z ' in place of - 1
let arr = new Array ( 33 ) ; function balTernary ( ter ) { let carry = 0 , base = 10 ; let i = 32 ; while ( ter > 0 ) { let rem = ter % base ; rem = rem + carry ; if ( rem == 0 ) { arr [ i -- ] = 0 ; carry = 0 ; } else if ( rem == 1 ) { arr [ i -- ] = 1 ; carry = 0 ; } else if ( rem == 2 ) { arr [ i -- ] = - 1 ; carry = 1 ; } else if ( rem == 3 ) { arr [ i -- ] = 0 ; carry = 1 ; } ter = Math . floor ( ter / base ) ; } if ( carry == 1 ) arr [ i ] = 1 ; } function ternary ( number ) { let ans = 0 , rem = 1 , base = 1 ; while ( number > 0 ) { rem = number % 3 ; ans = ans + rem * base ; number = Math . floor ( number / 3 ) ; base = base * 10 ; } return ans ; } let number = 3056 ; let ter = ternary ( number ) ; for ( let i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = 0 ; } balTernary ( ter ) ; let i = 0 ; while ( arr [ i ] == 0 ) { i ++ ; } for ( let j = i ; j <= 32 ; j ++ ) { if ( arr [ j ] == - 1 ) document . write ( ' ' ) ; else document . write ( arr [ j ] ) ; }