Stella Octangula Number | Returns value of n * ( 2 * n * n - 1 ) ; Finds if a value of f ( n ) is equal to x where n is in interval [ low . . high ] ; Returns true if x isStella octangula number . Else returns false . ; Find ' high ' for binary search by repeated doubling ; If condition is satisfied for a power of 2. ; Call binary search ; Driver code
def f ( n ) : NEW_LINE INDENT return n * ( 2 * n * n - 1 ) ; NEW_LINE DEDENT def binarySearch ( low , high , x ) : NEW_LINE INDENT while ( low <= high ) : NEW_LINE INDENT mid = int ( ( low + high ) // 2 ) ; NEW_LINE if ( f ( mid ) < x ) : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT elif ( f ( mid ) > x ) : NEW_LINE INDENT high = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT def isStellaOctangula ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT i = 1 ; NEW_LINE while ( f ( i ) < x ) : NEW_LINE INDENT i = i * 2 ; NEW_LINE DEDENT if ( f ( i ) == x ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return binarySearch ( i / 2 , i , x ) ; NEW_LINE DEDENT n = 51 ; NEW_LINE if ( isStellaOctangula ( n ) == True ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT