Python triangle quest, arithmetical operation inside print

前端 未结 2 827
迷失自我
迷失自我 2021-01-07 06:23

I try solve this problem I know that, I can do that like

for i in range(1,input()): 
    print int(str(i)*i)

It works, but I can\'t use str

相关标签:
2条回答
  • 2021-01-07 06:47

    I tried doing it this way (notice I took floor division here):

    for i in range(1,int(input())):
        print(i*((10**i)//9))
    

    for input 5, it yields:

    1
    22
    333
    4444
    
    0 讨论(0)
  • 2021-01-07 06:52

    Something that helps here is the equation for a "Repunit". Taking the equation for the i'th Repunit from that Wikipedia page, and substituting 10 in for b (base 10), we get the equation (10**i - 1) / (10 - 1) or (10**i - 1) / 9. This results in the sequence: 1, 11, 111, 1111.... Multiplying by i, we achieve the desired result:

    >>> for i in range(1,input()):
    ...    print i * (10**i - 1) / 9
    5<Enter>
    1 
    22
    333
    4444
    
    0 讨论(0)
提交回复
热议问题