How does one add the digits of a large number?

前端 未结 3 1778
你的背包
你的背包 2021-01-27 18:12

I have been trying to add the individual digits of a larger number for a while now, and I\'m having some trouble. I was wondering if anyone could help me.

For example, s

3条回答
  •  孤城傲影
    2021-01-27 18:45

    Treat it as a string, and sum the individual numbers. Slice if you need to.

    sum(map(int,str(12345)))
    Out[183]: 15
    
    sum(map(int,str(12345)[1:3]))
    Out[184]: 5
    

提交回复
热议问题