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
< ? php function countOdd ( $ n ) { $ coun = 0 ; for ( $ i = $ n - 2 ; $ i >= 1 ; $ i -- ) { if ( $ i & 1 ) { $ m = ( $ n - $ i ) / 2 ; $ j = ( $ i * ( $ i + 1 ) ) / 2 ; $ coun += $ j * $ m ; } else { $ m = ( ( $ n - 1 ) - $ i ) / 2 ; $ j = ( $ i * ( $ i + 1 ) ) / 2 ; $ coun += $ j * $ m ; } } return $ coun ; } function countEven ( $ n ) { $ coun = 0 ; for ( $ i = $ n - 2 ; $ i >= 1 ; $ i -- ) { if ( $ i & 1 ) { $ m = ( ( $ n - 1 ) - i ) / 2 ; $ j = ( $ i * ( $ i + 1 ) ) / 2 ; $ coun += $ j * $ m ; } else { $ m = ( $ n - $ i ) / 2 ; $ j = ( $ i * ( $ i + 1 ) ) / 2 ; $ coun += $ j * $ m ; } } return $ coun ; } $ n = 5 ; if ( $ n & 1 ) echo countOdd ( $ n ) ; else echo countEven ( $ n ) ; ? >