Ulam Number Sequence
Last Updated :
12 Aug, 2022
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.
For example,
- 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
Examples:
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:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 10000
vector< int > arr;
void ulam()
{
arr.push_back(1);
arr.push_back(2);
for ( int i = 3; i < MAX; i++) {
int count = 0;
for ( int j = 0; j < arr.size() - 1; j++) {
for ( int k = j + 1; k < arr.size(); k++) {
if (arr[j] + arr[k] == i) {
count++;
}
if (count > 1)
break ;
}
if (count > 1)
break ;
}
if (count == 1) {
arr.push_back(i);
}
}
}
int main()
{
ulam();
int n = 9;
cout << arr[n - 1];
return 0;
}
|
Java
import java.util.*;
class GFG {
static final int MAX = 1000 ;
static Vector<Integer> arr = new Vector<Integer>();
static void ulam()
{
arr.add( 1 );
arr.add( 2 );
for ( int i = 3 ; i < MAX; i++) {
int count = 0 ;
for ( int j = 0 ; j < arr.size() - 1 ; j++) {
for ( int k = j + 1 ; k < arr.size(); k++) {
if (arr.get(j) + arr.get(k) == i) {
count++;
}
if (count > 1 )
break ;
}
if (count > 1 )
break ;
}
if (count == 1 ) {
arr.add(i);
}
}
}
public static void main(String[] args)
{
ulam();
int n = 9 ;
System.out.println(arr.get(n - 1 ));
}
}
|
Python3
MAX = 1000
arr = []
def ulam():
arr.append( 1 );
arr.append( 2 );
for i in range ( 3 , MAX ):
count = 0 ;
for j in range ( len (arr) - 1 ):
for k in range (j + 1 , len (arr)):
if (arr[j] + arr[k] = = i):
count + = 1
if (count > 1 ):
break ;
if (count > 1 ):
break ;
if (count = = 1 ):
arr.append(i);
if __name__ = = '__main__' :
ulam();
n = 9 ;
print (arr[n - 1 ])
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly int MAX = 1000;
static List< int > arr = new List< int >();
static void ulam()
{
arr.Add(1);
arr.Add(2);
for ( int i = 3; i < MAX; i++)
{
int count = 0;
for ( int j = 0; j < arr.Count - 1; j++)
{
for ( int k = j + 1; k < arr.Count; k++)
{
if (arr[j] + arr[k] == i)
{
count++;
}
if (count > 1)
break ;
}
if (count > 1)
break ;
}
if (count == 1)
{
arr.Add(i);
}
}
}
public static void Main(String[] args)
{
ulam();
int n = 9;
Console.WriteLine(arr[n - 1]);
}
}
|
Javascript
<script>
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]);
</script>
|
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:
C++
#include <bits/stdc++.h>
using namespace std;
#define MAX 10000
vector< int > arr;
void ulam()
{
unordered_set< int > s;
arr.push_back(1);
s.insert(1);
arr.push_back(2);
s.insert(2);
for ( int i = 3; i < MAX; i++) {
int count = 0;
for ( int j = 0; j < arr.size(); j++) {
if (s.find(i - arr[j]) != s.end() && arr[j] != (i - arr[j]))
count++;
if (count > 2)
break ;
}
if (count == 2) {
arr.push_back(i);
s.insert(i);
}
}
}
int main()
{
ulam();
int n = 9;
cout << arr[n - 1];
return 0;
}
|
Java
import java.util.*;
class GFG {
static final int MAX = 10000 ;
static Vector<Integer> arr = new Vector<Integer>();
static void ulam()
{
Set<Integer> s = new HashSet<Integer>();
arr.add( 1 );
s.add( 1 );
arr.add( 2 );
s.add( 2 );
for ( int i = 3 ; i < MAX; i++) {
int count = 0 ;
for ( int j = 0 ; j < arr.size(); j++) {
if (s.contains(i - arr.get(j)) && arr.get(j) != (i - arr.get(j)))
count++;
if (count > 2 )
break ;
}
if (count == 2 ) {
arr.add(i);
s.add(i);
}
}
}
public static void main(String[] args)
{
ulam();
int n = 9 ;
System.out.println(arr.get(n - 1 ));
}
}
|
Python3
MAX = 10000
arr = []
def ulam():
s = set ()
arr.append( 1 )
s.add( 1 )
arr.append( 2 )
s.add( 2 )
for i in range ( 3 , MAX ):
count = 0
for j in range ( 0 , len (arr)):
if (i - arr[j]) in s and arr[j] ! = (i - arr[j]):
count + = 1
if count > 2 :
break
if count = = 2 :
arr.append(i)
s.add(i)
if __name__ = = "__main__" :
ulam()
n = 9
print (arr[n - 1 ])
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly int MAX = 10000;
static List< int > arr = new List< int >();
static void ulam()
{
HashSet< int > s = new HashSet< int >();
arr.Add(1);
s.Add(1);
arr.Add(2);
s.Add(2);
for ( int i = 3; i < MAX; i++)
{
int count = 0;
for ( int j = 0; j < arr.Count; j++)
{
if (s.Contains(i - arr[j]) &&
arr[j] != (i - arr[j]))
count++;
if (count > 2)
break ;
}
if (count == 2)
{
arr.Add(i);
s.Add(i);
}
}
}
public static void Main(String[] args)
{
ulam();
int n = 9;
Console.WriteLine(arr[n - 1]);
}
}
|
Javascript
let MAX = 10000
let arr = []
function ulam()
{
let s = new Set()
arr.push(1)
s.add(1)
arr.push(2)
s.add(2)
for ( var i = 3; i < MAX; i++)
{
let count = 0
for ( var j = 0; j < arr.length; j++)
{
if ( s.has(i - arr[j]) && arr[j] != (i - arr[j]))
count += 1
if (count > 2)
break
}
if (count == 2)
{
arr.push(i)
s.add(i)
}
}
}
ulam()
let n = 9
console.log(arr[n - 1])
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...