Number of different cyclic paths of length N in a tetrahedron | Function to count the number of steps in a tetrahedron ; Initially coming to B is B -> B ; Cannot reach A , D or C ; Iterate for all steps ; recurrence relation ; Memoize previous values ; Returns steps ; Driver code
function countPaths ( n ) { let zB = 1 ; let zADC = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { let nzB = zADC * 3 ; let nzADC = ( zADC * 2 + zB ) ; zB = nzB ; zADC = nzADC ; } return zB ; } let n = 3 ; document . write ( countPaths ( n ) ) ;