简体   繁体   English

我如何优化这个寻找数字X的代码,其数字的总和等于n?

[英]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 找到一个数字X,其数字之和等于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... 但似乎只有很少的数字与该属性......

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何优化这个递归 function 来计算其数字等于总和的 n 位十六进制数的数量? - How do I optimize this recursive function that counts the number of n-digit hexadecimal numbers whose digits equal a sum? 如何获得总和等于M的N个随机整数 - How to get N random integer numbers whose sum is equal to M 如何获取 4 位数字的所有可能组合的列表,其单个数字总和为 13,最后一位数字为 5 - how get list of all the possible combinations of 4 digit numbers whose individual digit sum is 13 and last digit is 5 in that number 如何打印列表的元素,其总和等于 python 中的给定数字? - how to print elements of a list whose sum is equal to a given number in python? 是否有任何 python 模块将数字 N 分成 3 个部分,使得这些部分的总和等于 N? 或者怎么做? - Is there any python module to divide a number N into 3 parts such that sum of that parts equal to N? or how to do that? 给定一个由 N 个整数组成的数组和一个整数 K,找出该数组中总和等于 K 的元素对的数量 - Given an array of N integers, and an integer K, find the number of pairs of elements in the array whose sum is equal to K select 来自 pandas 列的 n 个元素,其总和等于提供的数字 - select n elements from a pandas column whose sum is equal to a provided number 如何优化此Python代码? - How do I optimise this Python code? 前 n 位可被 n 整除的 10 位数字 - 10 digit number whose first n digits are divisible by n 从4个给定的数组(未排序)中,找到每个数组的元素,其总和等于某个数字X. - From 4 given arrays(not sorted), find the elements from each array whose sum is equal to some number X
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM