简体   繁体   English

python - 如何在数字的恒定总和等于9 python的数字序列中找到特定数字?

[英]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.

相关问题 如何找到它们的总和等于给定数字的数字? - How to find the numbers that their sum equal to a given number? Python程序找到等于n的连续数字值之和? - Python program find sum of consecutive number values equal to number n? python中某个数字的数字和 - Digit sum of certain number in python 如何将数字的总和除以数字? - how to divide sum of number by digit on number? python:搜索5位数字而不是特定数字 - python: search for a 5 digit number instead of specific number 如何在python中反复查找每个T数的数字总和的数字总和,直到成为单个数字? - how to find the sum of digits of sum of digits of each of T numbers repeatedly in python till it gets to being a single digit number? 我如何优化这个寻找数字X的代码,其数字的总和等于n? - how do i optimise this code of finding a number X whose sum with its digit is equal to n? 如何在python中查找列表的序列号(如行号1、2) - How to find sequence number of list in python (like line number 1, 2) 如何打印列表的元素,其总和等于 python 中的给定数字? - how to print elements of a list whose sum is equal to a given number in python? 如何检查一个数字的每个数字是否大于或等于另一个数字? - How to check every digit of a number is greater or equal than another number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM