Coders Packet

Sum of Connell Sequence In Python

By Harshith Appari

Hello Everyone, in this tutorial, we will see what the Connell sequence means and how to write the program of the Connell sequence by using Python.

First we have know about what is meant by sequence .

Sequence : Sequence is nothing but the numbers or alphabets in a specific order.

Now let us see about the connell sequence and how it works in the following statements.

The  Connell sequence looks like 1 2 4 5 7 9 ....

So we know the numbers sequence is 1 2 3 4 5....

so in Connell sequence 1 -> 1 (remains unchanged)

The Second number     2  -> 2 4 ( 2 is even and in the even sequence of the numbers the first numbers are 2 and 4)

The Third number        3  -> 5 7 9 ( 3 is the odd number and odd sequence of the numbers the first are 1 3 5 7 9... but 1 is present and 3 is considered as odd so next 3 elements are odd are 5 7 9)

This process will continue soon

Atlast we are adding the elements in the connell sequence and prints the result

The program is given below :

 

 

n = int(input("Enter the number till you want the series : "))
l=[]
s=0
final=[]
for i in range(1,n+1):
    l.append(i)
for i in l:
    l1=[]
    l1.append(i**2)
    while len(l1)!=i:
        l1.insert(0,l1[0]-2)
    for i in l1:
        final.append(i)
for i in range(0,n):
    s=s+final[i]
print("Sum of the Connell Sequence is = ",s)
print("The Sequence is :")
for i in range(0,n):
    print(final[i],end=" ")

 

 

output:

Enter the number till you want the series : 15
Sum of the Connell Sequence is =  185
The Sequence is :
1 2 4 5 7 9 10 12 14 16 17 19 21 23 25 

 

We accept the list length in the first line to print the Connell Sequence.

We are creating the two empty lists ( l and final ) where the final is used to store the Connell sequence, and l represents the current element.

We are creating a variable s = 0 where it is a sum variable.

First, with the help of for loop, we are appending the numbers from 1 to 15 in list l.

With the help of for loop, we are creating an empty list inside it l1, and we are appending the square of the number.

The logic part starts here: if the element in the list l and the length of l1 are identical, then it will directly append to the final list.

If they are not equal, then append the (first element in the list1(l1[0])-2) in the first position of the list l1.

And Now, by using the for loop, we can add the elements to the s variable.

Here the list of final elements is printed in one line. This can be done by using ( end=" " ).

And Now, finally printing the list of Connell Sequence and sum.

Download Complete Code

Comments

No comments yet