Count the number of ordered sets not containing consecutive numbers | Function to calculate the count of ordered set for a given size ; Base cases ; Function returns the count of all ordered sets ; Prestore the factorial value ; Iterate all ordered set sizes and find the count for each one maximum ordered set size will be smaller than N as all elements are distinct and non consecutive ; Multiply ny size ! for all the arrangements because sets are ordered ; Add to total answer ; Driver code
def CountSets ( x , pos ) : NEW_LINE INDENT if ( x <= 0 ) : NEW_LINE INDENT if ( pos == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( pos == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT answer = ( CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) ) NEW_LINE return answer NEW_LINE DEDENT def CountOrderedSets ( n ) : NEW_LINE INDENT factorial = [ 1 for i in range ( 10000 ) ] NEW_LINE factorial [ 0 ] = 1 NEW_LINE for i in range ( 1 , 10000 , 1 ) : NEW_LINE INDENT factorial [ i ] = factorial [ i - 1 ] * i NEW_LINE DEDENT answer = 0 NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT sets = CountSets ( n , i ) * factorial [ i ] NEW_LINE answer = answer + sets NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( CountOrderedSets ( N ) ) NEW_LINE DEDENT