Ulam Number Sequence | Javascript 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 ; Pre compute Ulam Number sequence ; Print nth Ulam number
var MAX = 10000 ; var arr = [ ] ; function ulam ( ) { arr . push ( 1 ) ; arr . push ( 2 ) ; for ( var i = 3 ; i < MAX ; i ++ ) { var count = 0 ; for ( var j = 0 ; j < arr . length - 1 ; j ++ ) { for ( var k = j + 1 ; k < arr . length ; k ++ ) { if ( arr [ j ] + arr [ k ] == i ) { count ++ ; } if ( count > 1 ) break ; } if ( count > 1 ) break ; } if ( count == 1 ) { arr . push ( i ) ; } } } ulam ( ) ; var n = 9 ; document . write ( arr [ n - 1 ] ) ;