📜  乌兰数序

📅  最后修改于: 2021-04-21 21:35:35             🧑  作者: Mango

给定正整数n ,任务是打印nUlam Number序列号
Ulam数在数学中,Ulam数是一个以术语U 1 = 1U 1 = 2开头的整数序列的成员,然后对于每个n> 2,定义的U n是大于U n的最小正整数。 -1可以用一种确切的方式表示为序列中两个不同的较早项的总和。
例如,

  • 3是一个Ulam数,因为它可以以一种完全相同的方式表示为两个不同的较早术语的总和
    (即1 + 2)
  • 4也是Ulam编号。除了(1 + 3)以外,我们还可以将4表示为(2 + 2),但在(2 + 2)项中没有区别。所以我们只有一种方法
  • 5可以两种方式表示为序列中两个不同的较早项的总和
    如(1 + 4)和(2 + 3),所以5不是Ulam Number

Ulam数字序列的前几项是-

例子:

Input  : 5
Output : 6

Input : 9
Output : 16

打印Ulam数列的n项的一种简单解决方案是生成直到n完整的ulam序列并打印n项,因为我们无法直接计算n项。
生成乌拉姆数字序列的方法:

  • As,我们具有序列的前两个项,分别为U 1 = 1和U 2 = 2。我们可以从i = 3开始搜索下一个Ulam数字。
  • 对于’i’的每个值,使用两个循环遍历该序列的较早项,并检查是否在添加两个不同的序列项时是否可以以一种完全相同的方式获得和为i
  • 如果是,则“ i”是下一个Ulam编号,将其存储。然后搜索下一个乌拉姆编号
  • 如果不是,则增加“ i”并重复相同的步骤
  • 继续搜索ulam数,直到获得第n个词

下面是上述想法的实现:

C++
// CPP code to print nth
// Ulam number
 
#include 
using namespace std;
 
#define MAX 10000
 
// Array to store Ulam Number
vector arr;
 
// function to compute ulam Number
void ulam()
{
    // push First 2 two term of the sequence
    // in the array
    // for further calculation
 
    arr.push_back(1);
 
    arr.push_back(2);
 
    // loop to generate Ulam number
    for (int i = 3; i < MAX; i++) {
 
        int count = 0;
 
        // traverse the array and check if
        // i can be reprsented as sum of
        // two distinct element of the array
 
        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 is 1 that means
        // i can be represented as sum of
        // two distinct terms of the sequence
 
        if (count == 1) {
            // i is ulam number
            arr.push_back(i);
        }
    }
}
 
// Driver code
int main()
{
    // Pre compute Ulam Number sequence
    ulam();
 
    int n = 9;
 
    // Print nth Ulam number
    cout << arr[n - 1];
 
    return 0;
}


Java
// JAVA code to print nth
// Ulam number
 
import java.util.*;
class GFG {
 
    static final int MAX = 1000;
 
    // Array to store Ulam Number
    static Vector arr = new Vector();
 
    // Function to compute ulam Number
    static void ulam()
    {
 
        // push First 2 two term of the sequence
        // in the array
        // for further calculation
 
        arr.add(1);
 
        arr.add(2);
 
        // loop to generate Ulam number
        for (int i = 3; i < MAX; i++) {
 
            int count = 0;
 
            // traverse the array and check if
            // i can be reprsented as sum of
            // two distinct element of the array
 
            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 is 2 that means
            // i can be represented as sum of
            // two distinct terms of the sequence
 
            if (count == 1) {
                // i is ulam number
                arr.add(i);
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // pre compute Ulam Number sequence
        ulam();
 
        int n = 9;
 
        // print nth Ulam number
        System.out.println(arr.get(n - 1));
    }
}


Python3
# Python3 code to print nth
# Ulam number
MAX = 1000
 
# Array to store Ulam Number
arr = []
 
# function to compute ulam Number
def ulam():
 
    # push First 2 two term of the sequence
    # in the array
    # for further calculation
    arr.append(1);
    arr.append(2);
 
    # loop to generate Ulam number
    for i in range(3, MAX):
        count = 0;
 
        # traverse the array and check if
        # i can be reprsented as sum of
        # two distinct element of the array
        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 is 1 that means
        # i can be represented as sum of
        # two distinct terms of the sequence
        if (count == 1):
           
            # i is ulam number
            arr.append(i);
         
# Driver code
if __name__=='__main__':
 
    # Pre compute Ulam Number sequence
    ulam();
    n = 9;
 
    # Print nth Ulam number
    print(arr[n - 1])
 
# This code is contributed by rutvik_56.


C#
// C# code to print nth
// Ulam number
using System;
using System.Collections.Generic;
 
class GFG
{
    static readonly int MAX = 1000;
 
    // Array to store Ulam Number
    static List arr = new List();
 
    // Function to compute ulam Number
    static void ulam()
    {
 
        // push First 2 two term of 
        // the sequence in the array
        // for further calculation
 
        arr.Add(1);
        arr.Add(2);
 
        // loop to generate Ulam number
        for (int i = 3; i < MAX; i++)
        {
            int count = 0;
 
            // traverse the array and check if
            // i can be reprsented as sum of
            // two distinct element of the array
            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 is 2 that means
            // i can be represented as sum of
            // two distinct terms of the sequence
            if (count == 1)
            {
                // i is ulam number
                arr.Add(i);
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // pre compute Ulam Number sequence
        ulam();
 
        int n = 9;
 
        // print nth Ulam number
        Console.WriteLine(arr[n - 1]);
    }
}
 
// This code is contibuted by Rajput-JI


C++
// Cpp code to print nth
// Ulam number
 
#include 
using namespace std;
 
#define MAX 10000
 
// Array to store Ulam Number
vector arr;
 
// function to compute ulam Number
void ulam()
{
 
    // Set to search specific Ulam number efficiently
    unordered_set s;
 
    // push First 2 two term of the sequence
    // in the array and set
    // for further calculation
 
    arr.push_back(1);
    s.insert(1);
 
    arr.push_back(2);
    s.insert(2);
 
    // loop to generate Ulam number
    for (int i = 3; i < MAX; i++) {
 
        int count = 0;
 
        // traverse the array and check if
        // i can be reprsented as sum of
        // two distinct element of the array
 
        for (int j = 0; j < arr.size(); j++) {
 
            // Check if i-arr[j] exist in the array or not using set
            // If yes, Then i can be represented as
            // sum of arr[j] + (i- arr[j])
 
            if (s.find(i - arr[j]) != s.end() && arr[j] != (i - arr[j]))
                count++;
 
            // if Count is greater than 2
            // break the loop
            if (count > 2)
                break;
        }
 
        // If count is 2 that means
        // i can be represented as sum of
        // two distinct terms of the sequence
 
        if (count == 2) {
            // i is ulam number
            arr.push_back(i);
            s.insert(i);
        }
    }
}
 
// Driver code
int main()
{
    // pre compute Ulam Number sequence
    ulam();
 
    int n = 9;
 
    // print nth Ulam number
    cout << arr[n - 1];
 
    return 0;
}


Java
// JAVA code to print nth
// Ulam number
 
import java.util.*;
class GFG {
 
    static final int MAX = 10000;
 
    // Array to store Ulam Number
    static Vector arr = new Vector();
 
    // function to compute ulam Number
    static void ulam()
    {
 
        // Set to search specific Ulam number efficiently
        Set s = new HashSet();
 
        // push First 2 two term of the sequence
        // in the array and set
        // for further calculation
 
        arr.add(1);
        s.add(1);
 
        arr.add(2);
        s.add(2);
 
        // loop to generate Ulam number
        for (int i = 3; i < MAX; i++) {
 
            int count = 0;
 
            // traverse the array and check if
            // i can be reprsented as sum of
            // two distinct element of the array
 
            for (int j = 0; j < arr.size(); j++) {
 
                // Check if i-arr[j] exist in the array or not using set
                // If yes, Then i can be represented as
                // sum of arr[j] + (i- arr[j])
 
                if (s.contains(i - arr.get(j)) && arr.get(j) != (i - arr.get(j)))
                    count++;
 
                // if Count is greater than 2
                // break the loop
                if (count > 2)
                    break;
            }
 
            // If count is 2 that means
            // i can be represented as sum of
            // two distinct terms of the sequence
 
            if (count == 2) {
                // i is ulam number
                arr.add(i);
                s.add(i);
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // pre compute Ulam Number sequence
        ulam();
 
        int n = 9;
 
        // print nth Ulam number
        System.out.println(arr.get(n - 1));
    }
}


Python3
# Python3 code to print nth Ulam number
MAX = 10000
 
# Array to store Ulam Number
arr = []
 
# function to compute ulam Number
def ulam():
 
    # Set to search specific Ulam
    # number efficiently
    s = set()
 
    # push First 2 two term of the
    # sequence in the array and set
    # for further calculation
    arr.append(1)
    s.add(1)
 
    arr.append(2)
    s.add(2)
 
    # loop to generate Ulam number
    for i in range(3, MAX):
 
        count = 0
 
        # traverse the array and check if
        # i can be reprsented as sum of
        # two distinct element of the array
        for j in range(0, len(arr)):
 
            # Check if i-arr[j] exist in the array
            # or not using set. If yes, Then i can
            # be represented as sum of arr[j] + (i- arr[j])
            if (i - arr[j]) in s and arr[j] != (i - arr[j]):
                count += 1
 
            # if Count is greater than 2
            # break the loop
            if count > 2:
                break
         
        # If count is 2 that means
        # i can be represented as sum of
        # two distinct terms of the sequence
        if count == 2:
            # i is ulam number
            arr.append(i)
            s.add(i)
         
# Driver Code   
if __name__ == "__main__":
 
    # pre compute Ulam Number sequence
    ulam()
 
    n = 9
 
    # print nth Ulam number
    print(arr[n - 1])
 
# This code is contributed by Rituraj Jain


C#
// C# code to print nth
// Ulam number
using System;
using System.Collections.Generic;
 
class GFG
{
 
    static readonly int MAX = 10000;
 
    // Array to store Ulam Number
    static List arr = new List();
 
    // function to compute ulam Number
    static void ulam()
    {
 
        // Set to search specific Ulam number efficiently
        HashSet s = new HashSet();
 
        // push First 2 two term of the sequence
        // in the array and set
        // for further calculation
        arr.Add(1);
        s.Add(1);
 
        arr.Add(2);
        s.Add(2);
 
        // loop to generate Ulam number
        for (int i = 3; i < MAX; i++)
        {
 
            int count = 0;
 
            // traverse the array and check if
            // i can be reprsented as sum of
            // two distinct element of the array
 
            for (int j = 0; j < arr.Count; j++)
            {
 
                // Check if i-arr[j] exist in the array
                // or not using set If yes,
                // Then i can be represented as
                // sum of arr[j] + (i- arr[j])
                if (s.Contains(i - arr[j]) &&
                    arr[j] != (i - arr[j]))
                    count++;
 
                // if Count is greater than 2
                // break the loop
                if (count > 2)
                    break;
            }
 
            // If count is 2 that means
            // i can be represented as sum of
            // two distinct terms of the sequence
            if (count == 2)
            {
                // i is ulam number
                arr.Add(i);
                s.Add(i);
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // pre compute Ulam Number sequence
        ulam();
 
        int n = 9;
 
        // print nth Ulam number
        Console.WriteLine(arr[n - 1]);
    }
}
 
// This code is contributed by Rajput-Ji


输出:
16

一种有效的解决方案是使用哈希表存储序列的早期编号,这样我们可以执行以下操作,而不是使用两个循环来检查“ i”是否可以表示为两个不同项之和。它在一个循环中。如果我们使用哈希表,搜索将很快。
以下是上述想法的实现:

C++

// Cpp code to print nth
// Ulam number
 
#include 
using namespace std;
 
#define MAX 10000
 
// Array to store Ulam Number
vector arr;
 
// function to compute ulam Number
void ulam()
{
 
    // Set to search specific Ulam number efficiently
    unordered_set s;
 
    // push First 2 two term of the sequence
    // in the array and set
    // for further calculation
 
    arr.push_back(1);
    s.insert(1);
 
    arr.push_back(2);
    s.insert(2);
 
    // loop to generate Ulam number
    for (int i = 3; i < MAX; i++) {
 
        int count = 0;
 
        // traverse the array and check if
        // i can be reprsented as sum of
        // two distinct element of the array
 
        for (int j = 0; j < arr.size(); j++) {
 
            // Check if i-arr[j] exist in the array or not using set
            // If yes, Then i can be represented as
            // sum of arr[j] + (i- arr[j])
 
            if (s.find(i - arr[j]) != s.end() && arr[j] != (i - arr[j]))
                count++;
 
            // if Count is greater than 2
            // break the loop
            if (count > 2)
                break;
        }
 
        // If count is 2 that means
        // i can be represented as sum of
        // two distinct terms of the sequence
 
        if (count == 2) {
            // i is ulam number
            arr.push_back(i);
            s.insert(i);
        }
    }
}
 
// Driver code
int main()
{
    // pre compute Ulam Number sequence
    ulam();
 
    int n = 9;
 
    // print nth Ulam number
    cout << arr[n - 1];
 
    return 0;
}

Java

// JAVA code to print nth
// Ulam number
 
import java.util.*;
class GFG {
 
    static final int MAX = 10000;
 
    // Array to store Ulam Number
    static Vector arr = new Vector();
 
    // function to compute ulam Number
    static void ulam()
    {
 
        // Set to search specific Ulam number efficiently
        Set s = new HashSet();
 
        // push First 2 two term of the sequence
        // in the array and set
        // for further calculation
 
        arr.add(1);
        s.add(1);
 
        arr.add(2);
        s.add(2);
 
        // loop to generate Ulam number
        for (int i = 3; i < MAX; i++) {
 
            int count = 0;
 
            // traverse the array and check if
            // i can be reprsented as sum of
            // two distinct element of the array
 
            for (int j = 0; j < arr.size(); j++) {
 
                // Check if i-arr[j] exist in the array or not using set
                // If yes, Then i can be represented as
                // sum of arr[j] + (i- arr[j])
 
                if (s.contains(i - arr.get(j)) && arr.get(j) != (i - arr.get(j)))
                    count++;
 
                // if Count is greater than 2
                // break the loop
                if (count > 2)
                    break;
            }
 
            // If count is 2 that means
            // i can be represented as sum of
            // two distinct terms of the sequence
 
            if (count == 2) {
                // i is ulam number
                arr.add(i);
                s.add(i);
            }
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // pre compute Ulam Number sequence
        ulam();
 
        int n = 9;
 
        // print nth Ulam number
        System.out.println(arr.get(n - 1));
    }
}

Python3

# Python3 code to print nth Ulam number
MAX = 10000
 
# Array to store Ulam Number
arr = []
 
# function to compute ulam Number
def ulam():
 
    # Set to search specific Ulam
    # number efficiently
    s = set()
 
    # push First 2 two term of the
    # sequence in the array and set
    # for further calculation
    arr.append(1)
    s.add(1)
 
    arr.append(2)
    s.add(2)
 
    # loop to generate Ulam number
    for i in range(3, MAX):
 
        count = 0
 
        # traverse the array and check if
        # i can be reprsented as sum of
        # two distinct element of the array
        for j in range(0, len(arr)):
 
            # Check if i-arr[j] exist in the array
            # or not using set. If yes, Then i can
            # be represented as sum of arr[j] + (i- arr[j])
            if (i - arr[j]) in s and arr[j] != (i - arr[j]):
                count += 1
 
            # if Count is greater than 2
            # break the loop
            if count > 2:
                break
         
        # If count is 2 that means
        # i can be represented as sum of
        # two distinct terms of the sequence
        if count == 2:
            # i is ulam number
            arr.append(i)
            s.add(i)
         
# Driver Code   
if __name__ == "__main__":
 
    # pre compute Ulam Number sequence
    ulam()
 
    n = 9
 
    # print nth Ulam number
    print(arr[n - 1])
 
# This code is contributed by Rituraj Jain

C#

// C# code to print nth
// Ulam number
using System;
using System.Collections.Generic;
 
class GFG
{
 
    static readonly int MAX = 10000;
 
    // Array to store Ulam Number
    static List arr = new List();
 
    // function to compute ulam Number
    static void ulam()
    {
 
        // Set to search specific Ulam number efficiently
        HashSet s = new HashSet();
 
        // push First 2 two term of the sequence
        // in the array and set
        // for further calculation
        arr.Add(1);
        s.Add(1);
 
        arr.Add(2);
        s.Add(2);
 
        // loop to generate Ulam number
        for (int i = 3; i < MAX; i++)
        {
 
            int count = 0;
 
            // traverse the array and check if
            // i can be reprsented as sum of
            // two distinct element of the array
 
            for (int j = 0; j < arr.Count; j++)
            {
 
                // Check if i-arr[j] exist in the array
                // or not using set If yes,
                // Then i can be represented as
                // sum of arr[j] + (i- arr[j])
                if (s.Contains(i - arr[j]) &&
                    arr[j] != (i - arr[j]))
                    count++;
 
                // if Count is greater than 2
                // break the loop
                if (count > 2)
                    break;
            }
 
            // If count is 2 that means
            // i can be represented as sum of
            // two distinct terms of the sequence
            if (count == 2)
            {
                // i is ulam number
                arr.Add(i);
                s.Add(i);
            }
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // pre compute Ulam Number sequence
        ulam();
 
        int n = 9;
 
        // print nth Ulam number
        Console.WriteLine(arr[n - 1]);
    }
}
 
// This code is contributed by Rajput-Ji
输出:
16