简体   繁体   English

重复添加一个数字的数字,直到 output 有一个数字

[英]Repeatedly adding digits of a number until the output has a single digit

I have a two digits number (eg 29) ) and wish to further reduce it to a single digit.我有一个两位数(例如 29))并希望进一步减少到一位数。 How can I do such in python?我怎么能在 python 中做到这一点? Shall I use the function inside the while loop?我应该在 while 循环中使用 function 吗? eg例如

29 -> 11 -> 2

result: [29,11,2]结果:[29,11,2]

x=input('Input digit: ')

result=0

box=[]


def add_two(x):
    bz=[]
    for i in str(x):
        bz.append(int(i))
        s=sum(bz)
        
    return s


box=[]    
a=0

while len(str(x))>1:

IIUC, you want to reduce a two digit number (29) into a single digit number by performing addition of the tens and units, repeatedly until the number is smaller than 10. IIUC,您想通过执行十位和单位的加法将两位数(29)减少为一位数,重复直到数字小于 10。

NB.注意。 I am using integers here, if you start from a string, first convert to int: x = int(x)我这里用的是整数,如果从字符串开始,先转换成int: x = int(x)

Let's use divmod by 10 to get the two components:让我们使用divmod by 10 来获得两个组件:

divmod(29, 10)
# 2, 9

and sum them:并将它们sum

sum(divmod(29, 10))
# 11

Now that we have the logic, let's repeat it:现在我们有了逻辑,让我们重复一遍:

x = 29
def reduce(x):
    return sum(divmod(x,10))

while x>9:
    x = reduce(x)
    
print(x)
# 2   # 2+9 -> 11 ; 1+1 -> 2
as a single function作为单个 function
def reduce_until(x):
    while x>9:
        x = sum(divmod(x,10))
    return x

reduce_until(29)
# 2
generic function for an input of any size:通用 function 用于任何大小的输入:
def reduce_until(x):
    while x>9:
        total = 0
        while x>0:
            x,r = divmod(x, 10)
            total += r
        x = total
    return x

reduce_until(56789)
# 56789 -> 35 -> 8

reduce_until(99999999999992)
# 99999999999992 -> 119 -> 11 -> 2
import math

dob_d = 29
while not (0 <= dob_d <= 9):
    n = 0
    for i in range(math.ceil(math.log10(dob_d))):
        n += int(dob_d / (10 ** i) % 10)
    dob_d = n

print(dob_d)

暂无
暂无

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

相关问题 在 python 中查找数字的总和直到总和变为单个数字 - Finding sum of digits of a number until sum becomes single digit in python 将月中某天的整数拆分为数字,重复求和直到获得一位数字整数 - Split day-of-month integers into digits, sum them repeatedly until get a single-digit integer 编写一个输入三位数的程序,在一行输出数字 - Writing a program that inputs a three digit number to output the digits on a single line 如何在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? 数字的乘积作为一位数字 - product of digits as a single-digit number 通过在 Python 2.7 中递归添加数字,在程序中获得错误的总和以将数字压缩为单个数字 - Getting wrong sum in program to condense a number to a single digit by adding the digits recursively in Python 2.7 给定 n,取 n 的数字之和。 如果该值超过一位,则继续减少产生一位数 - Given n, take tsum of the digits of n. If that value has more than one digit, continue reducing a single-digit number is produced 允许用户输入3位数字,然后输出该数字中的各个数字 - to allow the user to input a 3-digit number, and then output the individual digits in the number 我怎样才能得到与个位数一致的两位数的数字坐标列的最后一位 - How can i get the number coordinate column last digit of the double digits in line with the single digits 我将如何制作一个没有重复数字且第一位必须为 0 的 4 位数字? - How would I make a 4-digit number with no repeating digits and the first digit has to be 0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM