简体   繁体   中英

how do i optimise this code of finding a number X whose sum with its digit is equal to n?

Find a Number X whose sum with its digits is equal to N

n = int(input())
for i in range(n//2, n):

z = [int(x) for x in str(i)]
zz = sum(z)
if zz<=100:
    ans = int(i) + int(zz)
     if(int(i) + int(zz) == n) :
        print(i)

tile limit is exceeding

If it can be any number, this would be a fast way to do it.

i = int(input("Your number: "))
result = ""

while i > 9:
    result += "9"
    i -= 9

result += str(i)

print(result)

How about

for k in range(1000):

    if sum([int(i) for i in str(k)]) == k:
        print(k)

But there seem to be only very few numbers with that property...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM