[英]How to find a specific number in a sequence of number with a constant sum of digit equal to 9 python?
Enter the natural number N (1 <= N <= 1,000,000).输入自然数 N (1 <= N <= 1,000,000)。 Print to the screen the Nth number of the sequence: 9 18 27 36 45 54 63 72 81 90 108 117 (they all have their sum of digit equal to 9)
将序列的第 N 个数字打印到屏幕上:9 18 27 36 45 54 63 72 81 90 108 117(它们的数字总和都等于 9)
N = 9+(int(input())-1)*9
def sumnum(N):
sum = 0
while N > 0:
d = N%10
N = N//10
sum += d
sumnum(N)
while sum != 9:
N+=1
sumnum(N)
print(N)
Here's my code and it got this error这是我的代码,它得到了这个错误
TimeLimitError: Stopped on Line 4
Your sumnum
function doesn't return anything, so it never changes sum
(or any other value) outside of the function.您的
sumnum
函数不返回任何内容,因此它永远不会更改函数之外的sum
(或任何其他值)。 Normally you'd get an error trying to compare an undefined variable, but sum
is actually the name of a builtin function, so that's what you're comparing 9 to.通常你会在尝试比较一个未定义的变量时遇到错误,但
sum
实际上是一个内置函数的名称,所以这就是你要比较的 9 。
Here's a simple approach: iterate through all numbers in a while
loop, and check the sum of each one's digits by converting it to a string and summing the int values of its characters.这是一个简单的方法:在一个
while
循环中遍历所有数字,并通过将每个数字转换为字符串并将其字符的 int 值相加来检查每个数字的总和。 Each time you find one whose sum is 9, decrement N
by one.每次找到总和为 9 的一个时,将
N
减 1。 When it hits zero, print the current number.当它达到零时,打印当前数字。
>>> N = 12
>>> i = 0
>>> while True:
... i += 1
... if sum(int(d) for d in str(i)) == 9:
... N -= 1
... if N == 0:
... print(i)
... break
...
117
Here's a more code-golf-y approach using a filtered generator with itertools.count()
(less efficient for big numbers because it builds a list of the sequence up to N instead of just printing the last element):这是使用带有
itertools.count()
的过滤生成器的更多代码高尔夫-y 方法(对于大数字效率较低,因为它构建了最多 N 的序列列表,而不是仅打印最后一个元素):
>>> import itertools
>>> list(zip(range(N), (i for i in itertools.count() if sum(int(d) for d in str(i)) == 9)))[-1][-1]
117
This worked for me!这对我有用!
def getSum(n):
sum = 0
for digit in str(n):
sum += int(digit)
return sum
for i in range(0, 1000000):
n = i
sum = getSum(n)
if sum == 9:
print(f"{n} -- {sum}")
Performing arithmetic on strings can work but it's very inefficient.对字符串执行算术运算可以工作,但效率很低。 This may help (and performs very well).
这可能会有所帮助(并且表现非常好)。 Generates a list of all numbers in the inclusive range 1 -> 1,000,000 whose digits sum to 9. Of course, the range can be limited to 900,000 because that's the highest number less than one million that has this particular property.
生成包含范围 1 -> 1,000,000 的所有数字的列表,其数字总和为 9。当然,该范围可以限制为 900,000,因为这是具有此特定属性的小于 100 万的最大数字。
We can also take advantage of the fact that a number whose digits sum to 9 are also multiples of 9我们还可以利用这样一个事实,即数字之和为 9 的数也是 9 的倍数
import time
def sumdigits(n):
r = 0
while n > 0:
r += n % 10
n //= 10
return r
lon = []
start = time.perf_counter()
for i in range(9, 900001, 9):
if sumdigits(i) == 9:
lon.append(i)
end = time.perf_counter()
print(len(lon))
print(f'Duration = {end-start:.4f}')
Output:输出:
2002
Duration = 0.0531
Numbers whose digits add to 9 are multiples of 9 and vice versa.数字加到 9 的数字是 9 的倍数,反之亦然。 So the Nth number whose digits add to 9 is N*9.
所以数字加到9的第N个数是N*9。 So all you need is this:
所以你只需要这样:
N = int(input("Enter N: "))
print (N*9)
You can add range checking for N being between 1 and 1,000,000 if you like.如果您愿意,可以添加范围检查以检查 N 是否介于 1 和 1,000,000 之间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.