Python持续麻木

2024-06-07 12:06:31 发布

您现在位置:Python中文网/ 问答频道 /正文

需要一些帮助来用Python编写一个持久性数字程序来确定具有持久性的最小非负整数 (715--->;35--->;15--->;5),3,4,5,6,7。输出格式如下:

The smallest integer with a persistence of 3 is: 39
The smallest integer with a persistence of 4 is: xx
The smallest integer with a persistence of 5 is: xxx
The smallest integer with a persistence of 6 is: xxxx
The smallest integer with a persistence of 7 is: xxxxx

任何代码链接或线索将不胜感激,谢谢。在


Tags: ofthegt程序is格式with数字
1条回答
网友
1楼 · 发布于 2024-06-07 12:06:31
def main():

    low_limit = 3
    high_limit = 7

    for limit in range(low_limit, high_limit+1):
        number = 1
        while persistence(number) < limit:
            number += 1
            print('The smallest integer with a persistence of', limit, 'is:', number)

def persistence(x):
    pcount = 0    # counting the p from 0
    while x>=10:
        y=x       # temp copy of x 
        z=1       # z holds the value of y as being broken down and multplied
        while (y!=0):
            z=z*(y%10)     # stripping last number and mult by last z value
            y=y//10       # integer division, dropping off the last digit
        x=z    
        pcount +=1
    return pcount

main()

相关问题 更多 >

    热门问题