Number of odd and even results for every value of x in range [ min , max ] after performing N steps | Function that prints the number of odd and even results ; If constant at layer i is even , beven is true , otherwise false . If the coefficient of x at layer i is even , aeven is true , otherwise false . ; If any of the coefficients at any layer is found to be even , then the product of all the coefficients will always be even . ; Checking whether the constant added after all layers is even or odd . ; Assuming input x is even . ; Assuming input x is odd . ; Displaying the counts . ; Driver Code
function count_even_odd ( min , max , steps ) { var a , b , even , odd ; var beven = true , aeven = false ; var n = 2 ; for ( i = 0 ; i < n ; i ++ ) { a = steps [ i ] [ 0 ] ; b = steps [ i ] [ 1 ] ; if ( ! ( aeven || ( a & 1 ) > 0 ) ) aeven = true ; if ( beven ) { if ( ( b & 1 ) > 0 ) beven = false ; } else if ( ! ( ( a & 1 ) > 0 ) ) { if ( ! ( ( b & 1 ) > 0 ) ) beven = true ; } else { if ( ( b & 1 ) > 0 ) beven = true ; } } if ( beven ) { even = parseInt ( max / 2 ) - parseInt ( ( min - 1 ) / 2 ) ; odd = 0 ; } else { even = parseInt ( max / 2 ) - parseInt ( ( min - 1 ) / 2 ) ; odd = 0 ; } if ( ! ( beven ^ aeven ) ) even += max - min + 1 - parseInt ( max / 2 ) + parseInt ( ( min - 1 ) / 2 ) ; else odd += max - min + 1 - parseInt ( max / 2 ) + parseInt ( ( min - 1 ) / 2 ) ; document . write ( " " + even + " " + odd ) ; } var min = 1 , max = 4 ; var steps = [ [ 1 , 2 ] , [ 3 , 4 ] ] ; count_even_odd ( min , max , steps ) ;