📜  康奈尔序列

📅  最后修改于: 2021-04-29 14:33:52             🧑  作者: Mango

给定一个整数“ n”,生成康奈尔序列的前一个“ n”项。
Connell序列是由第一个奇数(即1作为其第一项)形成的序列。序列的后续项由前两个偶数(即2和4)组成,然后是后三个奇数(即5、7和9),再由后四个偶数(即10、12、14)组成和16等…。序列继续。

公式:

a[n] = 2 * n - floor((1 + sqrt(8 * n - 7))/2)   ; n > 1

例子:

Input : 6
Output : 1 2 4 5 7 9

Input : 12
Output : 1 2 4 5 7 9 10 12 14 16 17 19

在这里可能需要注意的是,用新行写的术语是,第一行的第一个术语,下一行的下两个术语,下一行的下三个术语,依此类推,给出了一个有趣的模式:

1:1
第2行:2 4
第3行:5 7 9
第4行:10 12 14 16
第5行:17 19 21 23 25
等等…
模式是特定行的每个最后一个数字等于该行号的平方。
例如

  1. 在第2行中,最后一个数字是4,等于其行数的平方,即2 ^ 2
  2. 在第5行中,最后一个数字为25,等于其行号的平方,即5 ^ 2

下面是一个简单的实现,我们通过交替添加奇数和偶数元素来生成结果。我们使用当前列表的大小来确定下一个要推送的元素数。

C++
// CPP code to generate first 'n' terms
// of Connell Sequence
#include 
using namespace std;
  
// Function to generate a fixed number
// of even or odd terms. The size of r
// decides whether numbers to be generated
// even or odd.
vector gen(long long int n,
                  vector r)
{
    long long int a = r[r.size() - 1];
    a++;
    for (int i = 1; i <= n; a += 2, i++)
        r.push_back(a);
    return r;
}
  
// Generating the first 'n' terms of
// Connell Sequence
vector conell(long long int n)
{
    vector res;
    long long int k = 1;
  
    // A dummy 0 is inserted at the
    // beginning for consistency
    res.push_back(0);
  
    while (1)
    {
        // Calling function gen() to generate
        // 'k' number of terms
        res = gen(k, res);
        k++;
  
        int j = res.size() - 1;
        while (j != n && j + k > n)
            k--;
  
        // Checking if 'n' terms are
        // already generated
        if (j >= n)
            break;
    }
  
    // Removing the previously inserted dummy 0
    res.erase(res.begin());
  
    return res;
}
  
// Driver Method
int main()
{
    long long int n = 10;
  
    cout << "The first " << n
         << " terms are" << endl;
    vector res = conell(n);
    for (int i = 0; i < res.size(); i++)
        cout << res[i] << " ";
    cout << endl;
    return 0;
}


Java
// Java code to generate 
// first 'n' terms 
// of Connell Sequence 
import java.util.*;
  
class GFG 
{
      
    // Function to generate a 
    // fixed number of even or 
    // odd terms. The size of r 
    // decides whether numbers 
    // to be generated even or odd. 
  
    static Vector gen(long n, Vector r) 
    {     
        long a = r.get(r.size() - 1);     
        a++;     
        for (int i = 1; i <= n; a += 2, i++) 
        {
            r.add(a);
        }     
        return r;     
    }
  
    // Generating the first 
    // 'n' terms of 
    // Connell Sequence 
    static Vector conell(long n) 
    {     
        Vector res = new Vector();     
        long k = 1;
  
        // A dummy 0 is inserted 
        // at the beginning for 
        // consistency 
        res.add(0L);     
          
        while (true)
        {
            // Calling function 
            // gen() to generate 
            // 'k' number of terms 
            res = gen(k, res);         
            k++;         
              
            int j = res.size() - 1;         
            while (j != n && j + k > n)
            {
                k--;
            }
  
            // Checking if 'n' 
            // terms are already 
            // generated 
            if (j >= n) 
            {
                break;
            }         
        }
  
        // Removing the previously 
        // inserted dummy 0 
        res.remove(0);     
          
        return res;     
    }
  
    // Driver Code 
    public static void main(String[] args)
    {
        long n = 10;     
          
        System.out.println("The first "
                    + n + " terms are");
                  
        Vector res = conell(n);     
        for (int i = 0; i < res.size(); i++)
        {
            System.out.print(res.get(i) + " ");
        }     
        System.out.println();     
    } 
} 
  
// This code has been contributed
// by Rajput-Ji


Python3
# Python3 code to generate first 'n' terms
# of Connell Sequence
  
# Function to generate a fixed number
# of even or odd terms. The size of r
# decides whether numbers to be generated
# even or odd.
def gen(n, r):
    a = r[-1]
    a += 1
    for i in range(1, n + 1):
        r.append(a)
        a += 2
    return r
  
# Generating the first 'n' terms of
# Connell Sequence
def conell(n):
    res = []
    k = 1
  
    # A dummy 0 is inserted at the
    # beginning for consistency
    res.append(0)
  
    while 1:
  
        # Calling function gen() to generate
        # 'k' number of terms
        res = gen(k, res)
        k += 1
        j = len(res) - 1
        while j != n and j + k > n:
            k -= 1
  
        # Checking if 'n' terms are
        # already generated
        if j >= n:
            break
  
    # Removing the previously inserted dummy 0
    res.remove(res[0])
    return res
  
# Driver Code
if __name__ == "__main__":
    n = 10
    print("The first %d terms are" % n)
    res = conell(n)
    for i in range(len(res)):
        print(res[i], end = " ")
    print()
  
# This code is contributed by
# sanjeev2552


C#
// C# code to generate
// first 'n' terms
// of Connell Sequence
using System;
using System.Collections.Generic;
  
class GFG
{
    // Function to generate a 
    // fixed number of even or 
    // odd terms. The size of r
    // decides whether numbers 
    // to be generated even or odd.
    static List gen(long n, 
                          List r)
    {
        long a = r[r.Count - 1];
        a++;
        for (int i = 1; i <= n; 
                 a += 2, i++)
            r.Add(a);
        return r;
    }
      
    // Generating the first
    // 'n' terms of
    // Connell Sequence
    static List conell(long n)
    {
        List res = new List();
        long k = 1;
      
        // A dummy 0 is inserted 
        // at the beginning for
        // consistency
        res.Add(0);
      
        while (true)
        {
            // Calling function 
            // gen() to generate
            // 'k' number of terms
            res = gen(k, res);
            k++;
      
            int j = res.Count - 1;
            while (j != n && 
                   j + k > n)
                k--;
      
            // Checking if 'n' 
            // terms are already 
            // generated
            if (j >= n)
                break;
        }
      
        // Removing the previously 
        // inserted dummy 0
        res.RemoveAt(0);
      
        return res;
    }
      
    // Driver Code
    static void Main()
    {
        long n = 10;
      
        Console.WriteLine("The first " + 
                      n + " terms are");
        List res = conell(n);
        for (int i = 0; i < res.Count; i++)
            Console.Write(res[i] + " ");
        Console.WriteLine();
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)


输出:

The first 10 terms are
1 2 4 5 7 9 10 12 14 16