Saturday, November 16, 2024
Google search engine
HomeData Modelling & AIConnell Sequence

Connell Sequence

Given an integer ‘n’, generate the first ‘n’ terms of the Connell Sequence. 
Connell Sequence is the sequence formed with the first odd number, i.e 1 as its first term. The subsequent terms of the sequence are made up of the first two even numbers, i.e 2 and 4, followed by the next three odd numbers, i.e 5, 7 and 9, followed by the next four even numbers, i.e 10, 12, 14 and 16 and so on …. the sequence continues.
Formula: 
 

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

Examples: 
 

Input : 6
Output : 1 2 4 5 7 9

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

 

Recommended Practice

It may be noted here that writing the terms in new lines as, first term in first line, next two terms in next line, next three terms in next line and so on, gives an interesting pattern as:
Line 1 : 1 
Line 2 : 2 4 
Line 3 : 5 7 9 
Line 4 : 10 12 14 16 
Line 5 : 17 19 21 23 25 
and so on… 
The pattern is every last number of a particular line is equal to that line number squared. 
For example 
 

  1. In line 2 last number is 4 which is equal to its line number squared, i.e 2^2
  2. In line 5 last number is 25 which is equal to its line number squared, i.e 5^2

Below is a simple implementation where we generate result by alternatively adding odd and even number of elements. We use size of current list to decide next number of elements to push.
 

C++




// CPP code to generate first 'n' terms
// of Connell Sequence
#include <bits/stdc++.h>
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<long long int> gen(long long int n,
                  vector<long long int> 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<long long int> connell(long long int n)
{
    vector<long long int> 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<long long int> 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<Long> gen(long n, Vector<Long> 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<Long> connell(long n)
    {    
        Vector<Long> res = new Vector<Long>();    
        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<Long> 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 connell(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<long> gen(long n,
                          List<long> 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<long> connell(long n)
    {
        List<long> res = new List<long>();
        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<long> 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)


Javascript




<script>
 
// Javascript 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.
function gen(n, r)
{
    var a = r[r.length - 1];
    a++;
    for (var i = 1; i <= n; a += 2, i++)
        r.push(a);
    return r;
}
 
// Generating the first 'n' terms of
// Connell Sequence
function connell(n)
{
    var res = [];
    var k = 1;
 
    // A dummy 0 is inserted at the
    // beginning for consistency
    res.push(0);
 
    while (1)
    {
        // Calling function gen() to generate
        // 'k' number of terms
        res = gen(k, res);
        k++;
 
        var j = res.length - 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.shift();
 
    return res;
}
 
// Driver Method
var n = 10;
document.write( "The first " + n
     + " terms are" + "<br>");
var res = conell(n);
for (var i = 0; i < res.length; i++)
    document.write( res[i] + " ");
 
// This code is contributed by rrrtnx.
</script>


Output: 
 

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

 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments