
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Connell sequence in Python
Suppose we have a number n, we have to find the nth term of Connell sequence. The Connell sequence is as follows: 1. Take first odd integer: 1 2. Take next two even integers 2, 4 3. Then take the next three odd integers 5, 7, 9 4. After that take the next four even integers 10, 12, 14, 16 And so on.
So, if the input is like 12, then the output will be 21
To solve this, we will follow these steps −
- i := 1
- while quotient of (i *(i + 1) / 2) < n + 1, do
- i := i + 1
- idx := i *(i + 1) / 2, take only quotient
- num := i^2
- return num - 2 *(idx - n - 1)
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, n): i = 1 while (i * (i + 1) // 2) < n + 1: i += 1 idx = i * (i + 1) // 2 num = i**2 return num - 2 * (idx - n - 1) ob = Solution() print(ob.solve(12))
Input
12
Output
21
- Related Articles
- Collatz sequence in Python
- Python Sequence Types
- Longest Consecutive Sequence in Python
- Python Text Sequence Types
- Python Binary Sequence Types
- Find maximum length Snake sequence in Python
- Program to find nth sequence after following the given string sequence rules in Python
- What is a sequence data type in Python?
- How to iterate by sequence index in Python?
- How to Merge Elements in a Python Sequence?
- Program to find number of ways we can select sequence from Ajob Sequence in Python
- Find element position in given monotonic sequence in Python
- Second most repeated word in a sequence in Python?
- Find bitonic point in given bitonic sequence in Python
- Program to find length of longest consecutive sequence in Python

Advertisements