Python triangle quest, arithmetical operation inside print

匿名 (未验证) 提交于 2019-12-03 02:29:01

问题:

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 strings, so i have idea to do that like

for i in range(1,input()):      sum+=10**i     print i*((1+sum)%10**i)  

for i=1   1*(1+10)%10,   for i=2   2*((1+110)%100),   for i=3   3*((1+1110)%1000) etc. 

But it doesn't work and have more than 2 lines :( Anyone have idea how to solve this problem ? Generally i know how to solve this problem mathematic but i don't know how to do that in python ( no use string and write no more than 2 lines)

回答1:

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 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!