Home > Blockchain >  Python | addition of pattern 1 22 333 4444 55555​ ....?
Python | addition of pattern 1 22 333 4444 55555​ ....?

Time:04-12

How do i do addition of 1 22 333 4444 55555​ ....?

i) case 1: n = 1, v = 1

ii) case 2: n= 2, v = 23 (Note: 23 is derived as 1 22)

def create_sequence():

   n = int(input('Enter the number till which you want the sequence:'))

   for i in range(1,n 1):

       print(str(i) * i)

   

create_sequence()

CodePudding user response:

You generate a sequence of elements of the form str(i)*i, which you cast into an integer, then sum that sequence.

def create_sequence(n):
    return sum(int(str(i)*i) for i in range(1, n 1))

create_sequence(int(input('Enter the number till which you want the sequence: ')))

CodePudding user response:

Take advantage of loops, they are your friend

def create_sequence():

   n = int(input('Enter the number till which you want the sequence:'))
   sum = 0
   for x in range(1, n 1):
       string = ""
       for i in range(1, x 1):
           string  = str(x)
       sum  = int(string)
   return sum
print(create_sequence())
  • Related