Ulam Number Sequence | Python3 code to print nth Ulam number ; Array to store Ulam Number ; function to compute ulam Number ; push First 2 two term of the sequence in the array for further calculation ; loop to generate Ulam number ; traverse the array and check if i can be represented as sum of two distinct element of the array ; If count is 1 that means i can be represented as sum of two distinct terms of the sequence ; i is ulam number ; Driver code ; Pre compute Ulam Number sequence ; Print nth Ulam number
MAX = 1000 NEW_LINE arr = [ ] NEW_LINE def ulam ( ) : NEW_LINE INDENT arr . append ( 1 ) ; NEW_LINE arr . append ( 2 ) ; NEW_LINE for i in range ( 3 , MAX ) : NEW_LINE INDENT count = 0 ; NEW_LINE for j in range ( len ( arr ) - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , len ( arr ) ) : NEW_LINE INDENT if ( arr [ j ] + arr [ k ] == i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( count > 1 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( count > 1 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( count == 1 ) : NEW_LINE INDENT arr . append ( i ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ulam ( ) ; NEW_LINE n = 9 ; NEW_LINE print ( arr [ n - 1 ] ) NEW_LINE DEDENT