Given a positive integer n , the task is to print nth Number of the Ulam Number Sequence
Ulam Number: In Mathematics, An Ulam number is the member of an Integer sequence that starts with the term U1 = 1 and U1 = 2 and then for every n > 2, Un is defined is smallest positive integer greater than Un-1 which can be expressed as sum of two distinct earlier term of the sequence in exactly one way.
- 3 is an Ulam Number as it can be expressed as sum of two distinct earlier terms in exactly one way
( i.e 1 + 2 )
- 4 is also an Ulam number. Other than ( 1 + 3) we can express 4 as ( 2 + 2 ) but in (2 + 2) terms are not distinct. So we have only one way
- 5 can be expressed as sum of two distinct earlier terms of the sequence in two ways
as ( 1 + 4 ) and ( 2 + 3), So, 5 is not an Ulam Number
The first few terms of the Ulam number sequence are-
1, 2, 3, 4, 6, 8, 11, 13, 16, 18, 26, 28, 36, 38, 47, 48, 53, 57, 62, 69, 72, 77, 82, 87, 97, 99, 102
Input : 5 Output : 6 Input : 9 Output : 16
A Simple Solution to print nth term of the Ulam number sequence is to generate entire ulam sequence up to n and print nth term, As we can not compute nth term directly.
Approach to generate ulam number sequence:
- As, We have first two term of the sequence as U1=1 and U2=2. we can start our search from i=3 for next Ulam number.
- For every value of ‘i’ traverse the earlier terms of the sequence using two loops and check if on adding two distinct term of sequence we can get sum as i in exactly one way or not
- If Yes, Then ‘i’ is next Ulam number, Store it. and then search for next ulam number
- If not, then increment ‘i’ and repeat the same
- Keep searching ulam number, until we get our nth term
Below is the implementation of the above idea:
An Efficient solution is to use a hash table to store the earlier number of the sequence so that instead of using two loops to check if ‘i’ can be expressed as the sum of two distinct terms in exactly one way or not, we can do it in a single loop. Searching will be fast if we will use a hash table.
Below is the Implementation of above idea:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- The Ulam Spiral
- Find the sequence number of a triangular number
- Find Nth number in a sequence which is not a multiple of a given number
- k-th number in the Odd-Even sequence
- Find if the given number is present in the infinite sequence or not
- Find a sequence of N prime numbers whose sum is a composite number
- Number of ways to divide string in sub-strings such to make them in lexicographically increasing sequence
- Count Possible Decodings of a given Digit Sequence
- Farey Sequence
- G-Fact 21 | Collatz Sequence
- Complete the sequence generated by a polynomial
- Juggler Sequence
- Padovan Sequence
- Aliquot Sequence
- Recaman's sequence
- Thue-Morse sequence
- Sum of the sequence 2, 22, 222, .........
- Sylvester's sequence
- Digit - Product - Sequence
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.