Count of distinct integers belonging to first N terms of at least one of given GPs | Function to find the count of distinct integers that belong to the first N terms of at least one of them is GP ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code
def UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) : NEW_LINE INDENT S = set ( ) NEW_LINE p1 = a1 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S . add ( p1 ) NEW_LINE p1 = ( p1 * r1 ) NEW_LINE DEDENT p2 = a2 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S . add ( p2 ) NEW_LINE p2 = ( p2 * r2 ) NEW_LINE DEDENT return len ( S ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE a1 = 3 NEW_LINE r1 = 2 NEW_LINE a2 = 2 NEW_LINE r2 = 3 NEW_LINE print ( UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ) NEW_LINE DEDENT