Count of distinct rectangles inscribed in an equilateral triangle | Function to return the count of rectangles when n is odd ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Calculating number of dots in vertical level ; Calculating number of ways to select two points in the horizontal level i ; Multiply both to obtain the number of rectangles formed at that level ; Function to return the count of rectangles when n is even ; Driver code ; If n is odd
function countOdd ( n ) { let coun = 0 , m , j , i ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( i >= 1 ) { m = parseInt ( ( n - i ) / 2 , 10 ) ; j = parseInt ( ( i * ( i + 1 ) ) / 2 , 10 ) ; coun += j * m ; } else { m = parseInt ( ( ( n - 1 ) - i ) / 2 , 10 ) ; j = parseInt ( ( i * ( i + 1 ) ) / 2 , 10 ) ; coun += j * m ; } } return coun ; } function countEven ( n ) { let coun = 0 , m , j , i ; for ( i = n - 2 ; i >= 1 ; i -- ) { if ( i >= 1 ) { m = parseInt ( ( ( n - 1 ) - i ) / 2 , 10 ) ; j = parseInt ( ( i * ( i + 1 ) ) / 2 , 10 ) ; coun += j * m ; } else { m = parseInt ( ( n - i ) / 2 , 10 ) ; j = parseInt ( ( i * ( i + 1 ) ) / 2 , 10 ) ; coun += j * m ; } } return coun ; } let n = 5 ; if ( n >= 1 ) document . write ( countOdd ( n ) ) ; else document . write ( countEven ( n ) ) ;