📜  Moser-de Bruijn序列

📅  最后修改于: 2021-04-24 04:18:03             🧑  作者: Mango

给定整数“ n”,打印Moser-de Bruijn序列的前“ n”项。

Moser-de Bruijn序列是通过将数字4的不同幂(例如1、4、16、64等)相加而获得的序列。

例子 :

Input : 5
Output : 0 1 4 5 16

Input : 10
Output : 0 1 4 5 16 17 20 21 64 65

可以看到,序列的项遵循递归关系:

1) S(2 * n) = 4 * S(n)
2) S(2 * n + 1) = 4 * S(n) + 1
with S(0) = 0 and S(1) = 1

在这里可以注意到,作为4的非幂的和的任何数字都不是该序列的一部分。例如,8并不是序列的一部分,因为它是由4的4和4的非唯一幂的总和形成的。
因此,序列中不存在的不是4的幂的任何数字都必须是4的不同幂的总和。
例如,21不是序列的一部分,即使它不是4的幂,因为它是4的不同幂的和,即1、4和16。

利用上面讨论的递归关系可以有效地生成序列。

C++
// CPP code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
#include 
using namespace std;
  
// Function to generate nth term 
// of Moser-de Bruijn Sequence
int gen(int n)
{ 
    // S(0) = 0
    if (n == 0)
        return 0;
      
    // S(1) = 1
    else if (n == 1)
        return 1;
      
    // S(2 * n) = 4 * S(n)
    else if (n % 2 == 0)
        return 4 * gen(n / 2);
      
    // S(2 * n + 1) = 4 * S(n) + 1
    else if (n % 2 == 1)
        return 4 * gen(n / 2) + 1;
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        cout << gen(i) << " ";
    cout << "\n";
}
  
// Driver Code
int main()
{
    int n = 15;
    cout << "First " << n << " terms of "
         << "Moser-de Bruijn Sequence : \n";
    moserDeBruijn(n);
    return 0;
}


Java
// Java code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
  
class GFG 
{
      
// Function to generate nth term 
// of Moser-de Bruijn Sequence
public static int gen(int n)
{ 
      
    // S(0) = 0
    if (n == 0)
        return 0;
      
    // S(1) = 1
    else if (n == 1)
        return 1;
      
    // S(2 * n) = 4 * S(n)
    else if (n % 2 == 0)
        return 4 * gen(n / 2);
      
    // S(2 * n + 1) = 4 * S(n) + 1
    else if (n % 2 == 1)
        return 4 * gen(n / 2) + 1;
    return 0;
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
public static void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        System.out.print(gen(i) + " ");
    System.out.println();
}
  
// Driver Code
public static void main(String args[])
{
    int n = 15;
    System.out.println("First " + n + 
                       " terms of " + 
      "Moser-de Bruijn Sequence : ");
    moserDeBruijn(n);
}
}
  
// This code is contributed by JaideepPyne.


Python3
# Python code to generate first 
# 'n' terms of the Moser-de 
# Bruijn Sequence
  
# Function to generate nth term
# of Moser-de Bruijn Sequence
def gen(n):
  
    # S(0) = 0
    if n == 0:
        return 0
  
    # S(1) = 1
    elif n ==1:
        return 1
  
    # S(2 * n) = 4 * S(n)
    elif n % 2 ==0:
        return 4 * gen(n // 2)
  
    # S(2 * n + 1) = 4 * S(n) + 1
    elif n % 2 == 1:
        return 4 * gen(n // 2) +1
  
# Generating the first 'n' terms
# of Moser-de Bruijn Sequence
def moserDeBruijn(n):
    for i in range(n):
        print(gen(i), end = " ")
  
# Driver Program
n = 15
print("First", n, "terms of ",
       "Moser-de Brujn Sequence:")
moserDeBruijn(n)
  
# This code is contributed by Shrikant13


C#
// C# code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
using System;
  
class GFG {
      
// Function to generate nth term 
// of Moser-de Bruijn Sequence
public static int gen(int n)
{ 
      
    // S(0) = 0
    if (n == 0)
        return 0;
      
    // S(1) = 1
    else if (n == 1)
        return 1;
      
    // S(2 * n) = 4 * S(n)
    else if (n % 2 == 0)
        return 4 * gen(n / 2);
      
    // S(2 * n + 1) = 4 * S(n) + 1
    else if (n % 2 == 1)
        return 4 * gen(n / 2) + 1;
    return 0;
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
public static void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        Console.Write(gen(i) + " ");
        Console.WriteLine();
}
  
// Driver Code
public static void Main()
{
    int n = 15;
    Console.WriteLine("First " + n + 
                    " terms of " + 
    "Moser-de Bruijn Sequence : ");
    moserDeBruijn(n);
}
}
  
// This code is contributed by anuj_67.


PHP


C++
// CPP code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
#include 
using namespace std;
  
// Function to generate nth term 
// of Moser-de Bruijn Sequence
int gen(int n)
{ 
    int S[n+1];
  
    S[0] = 0;
    S[1] = 1;
  
    for (int i = 2; i <= n; i++)
    {    
        // S(2 * n) = 4 * S(n)
        if (i % 2 == 0)
           S[i] = 4 * S[i / 2];
      
        // S(2 * n + 1) = 4 * S(n) + 1
        else 
           S[i] = 4 * S[i / 2] + 1;
    }
      
    return S[n];
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        cout << gen(i) << " ";
    cout << "\n";
}
  
// Driver Code
int main()
{
    int n = 15;
    cout << "First " << n << " terms of "
         << "Moser-de Bruijn Sequence : \n";
    moserDeBruijn(n);
    return 0;
}


Java
// Java code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
  
class GFG 
{
      
// Function to generate nth term 
// of Moser-de Bruijn Sequence
static int gen(int n)
{ 
    int []S = new int [n + 1];
  
    S[0] = 0;
    if(n != 0)
        S[1] = 1;
  
    for (int i = 2; i <= n; i++)
    { 
          
        // S(2 * n) = 4 * S(n)
        if (i % 2 == 0)
        S[i] = 4 * S[i / 2];
      
        // S(2 * n + 1) = 4 * S(n) + 1
        else
        S[i] = 4 * S[i/2] + 1;
    }
      
    return S[n];
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
static void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        System.out.print(gen(i)+" ");
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 15;
    System.out.println("First " + n + 
                       " terms of " + 
      "Moser-de Bruijn Sequence : ");
    moserDeBruijn(n);
}
}
  
// This code is contributed by 
// Smitha Dinesh Semwal.


python3
# python3 code to generate first 'n' terms 
# of the Moser-de Bruijn Sequence
  
# Function to generate nth term 
# of Moser-de Bruijn Sequence
def gen( n ):
    S = [0, 1]
    for i in range(2, n+1):
          
        # S(2 * n) = 4 * S(n)
        if i % 2 == 0:
            S.append(4 * S[int(i / 2)]);
              
        # S(2 * n + 1) = 4 * S(n) + 1
        else:
            S.append(4 * S[int(i / 2)] + 1);
    z = S[n];
    return z;
  
# Generating the first 'n' terms 
# of Moser-de Bruijn Sequence
def moserDeBruijn(n):
    for i in range(n):
        print(gen(i), end = " ")
  
# Driver Code
n = 15
print("First", n, "terms of ",
    "Moser-de Brujn Sequence:")
moserDeBruijn(n)
  
# This code is contributed by mits.


C#
// C# code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
using System;
  
class GFG 
{
      
// Function to generate nth term 
// of Moser-de Bruijn Sequence
static int gen(int n)
{ 
    int []S = new int [n + 1];
  
    S[0] = 0;
    if(n != 0)
        S[1] = 1;
  
    for (int i = 2; i <= n; i++)
    { 
          
        // S(2 * n) = 4 * S(n)
        if (i % 2 == 0)
        S[i] = 4 * S[i / 2];
      
        // S(2 * n + 1) = 4 * S(n) + 1
        else
        S[i] = 4 * S[i/2] + 1;
    }
      
    return S[n];
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
static void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        Console.Write(gen(i)+" ");
}
  
// Driver Code
public static void Main()
{
    int n = 15;
    Console.WriteLine("First " + n + 
                      " terms of " + 
     "Moser-de Bruijn Sequence : ");
    moserDeBruijn(n);
}
}
  
// This code is contributed by 
// Smitha Dinesh Semwal.


PHP


输出 :

First 15 terms of Moser-de Bruijn Sequence : 
0 1 4 5 16 17 20 21 64 65 68 69 80 81 84

动态编程实现:

C++

// CPP code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
#include 
using namespace std;
  
// Function to generate nth term 
// of Moser-de Bruijn Sequence
int gen(int n)
{ 
    int S[n+1];
  
    S[0] = 0;
    S[1] = 1;
  
    for (int i = 2; i <= n; i++)
    {    
        // S(2 * n) = 4 * S(n)
        if (i % 2 == 0)
           S[i] = 4 * S[i / 2];
      
        // S(2 * n + 1) = 4 * S(n) + 1
        else 
           S[i] = 4 * S[i / 2] + 1;
    }
      
    return S[n];
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        cout << gen(i) << " ";
    cout << "\n";
}
  
// Driver Code
int main()
{
    int n = 15;
    cout << "First " << n << " terms of "
         << "Moser-de Bruijn Sequence : \n";
    moserDeBruijn(n);
    return 0;
}

Java

// Java code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
  
class GFG 
{
      
// Function to generate nth term 
// of Moser-de Bruijn Sequence
static int gen(int n)
{ 
    int []S = new int [n + 1];
  
    S[0] = 0;
    if(n != 0)
        S[1] = 1;
  
    for (int i = 2; i <= n; i++)
    { 
          
        // S(2 * n) = 4 * S(n)
        if (i % 2 == 0)
        S[i] = 4 * S[i / 2];
      
        // S(2 * n + 1) = 4 * S(n) + 1
        else
        S[i] = 4 * S[i/2] + 1;
    }
      
    return S[n];
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
static void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        System.out.print(gen(i)+" ");
}
  
// Driver Code
public static void main(String[] args)
{
    int n = 15;
    System.out.println("First " + n + 
                       " terms of " + 
      "Moser-de Bruijn Sequence : ");
    moserDeBruijn(n);
}
}
  
// This code is contributed by 
// Smitha Dinesh Semwal.

python3

# python3 code to generate first 'n' terms 
# of the Moser-de Bruijn Sequence
  
# Function to generate nth term 
# of Moser-de Bruijn Sequence
def gen( n ):
    S = [0, 1]
    for i in range(2, n+1):
          
        # S(2 * n) = 4 * S(n)
        if i % 2 == 0:
            S.append(4 * S[int(i / 2)]);
              
        # S(2 * n + 1) = 4 * S(n) + 1
        else:
            S.append(4 * S[int(i / 2)] + 1);
    z = S[n];
    return z;
  
# Generating the first 'n' terms 
# of Moser-de Bruijn Sequence
def moserDeBruijn(n):
    for i in range(n):
        print(gen(i), end = " ")
  
# Driver Code
n = 15
print("First", n, "terms of ",
    "Moser-de Brujn Sequence:")
moserDeBruijn(n)
  
# This code is contributed by mits.

C#

// C# code to generate first 'n' terms 
// of the Moser-de Bruijn Sequence
using System;
  
class GFG 
{
      
// Function to generate nth term 
// of Moser-de Bruijn Sequence
static int gen(int n)
{ 
    int []S = new int [n + 1];
  
    S[0] = 0;
    if(n != 0)
        S[1] = 1;
  
    for (int i = 2; i <= n; i++)
    { 
          
        // S(2 * n) = 4 * S(n)
        if (i % 2 == 0)
        S[i] = 4 * S[i / 2];
      
        // S(2 * n + 1) = 4 * S(n) + 1
        else
        S[i] = 4 * S[i/2] + 1;
    }
      
    return S[n];
}
  
// Generating the first 'n' terms 
// of Moser-de Bruijn Sequence
static void moserDeBruijn(int n)
{
    for (int i = 0; i < n; i++)
        Console.Write(gen(i)+" ");
}
  
// Driver Code
public static void Main()
{
    int n = 15;
    Console.WriteLine("First " + n + 
                      " terms of " + 
     "Moser-de Bruijn Sequence : ");
    moserDeBruijn(n);
}
}
  
// This code is contributed by 
// Smitha Dinesh Semwal.

的PHP


输出 :

First 15 terms of Moser-de Bruijn Sequence : 
0 1 4 5 16 17 20 21 64 65 68 69 80 81 84