Python triangle quest, arithmetical operation inside print

前端 未结 2 948
迷失自我
迷失自我 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: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
    1 
    22
    333
    4444
    

提交回复
热议问题