【问题标题】:Python Persistance-ListPython 持久性列表
【发布时间】:2014-11-22 14:26:20
【问题描述】:

将整数的数字相乘并继续该过程会得到令人惊讶的结果,即乘积序列总是得到一个个位数的数字。例如: 715  35  15  5 88  64  24  8 27  14  4 达到个位数所需的产品数量称为持久性数量 那个整数。因此 715 和 88 的持久性数为 3,而 27 的持久性数为 2。 编写一个程序,找出唯一持久性大于 3 的两位数? 这就是问题所在。

for i in range(10,100):
    for l in range(i):
        numbers = list(str(l))
        num = int(numbers[0]) * int(numbers[1])

这就是我所拥有的。请帮忙

【问题讨论】:

    标签: python list


    【解决方案1】:

    答案是77,它转到49,它转到36,它转到18,它转到8

    def multiply_digits(x):
        total = 1
        for digit in str(x):
            total *= int(digit)
        return total
    
    for value in range(11, 100):
        new_value = multiply_digits(value)
        persistance = 1
        while new_value >= 10:
            new_value = multiply_digits(new_value)
            persistance += 1
            if persistance > 3:
                print value
    

    【讨论】:

      【解决方案2】:

      每个人都认为这是一个糟糕的问题,所以我为回答和帮助你作弊感到难过:( 但这很有趣! :) 解决问题是你聪明的标志,呵呵,而不是真正尝试是相反的标志。

      def newn(n):
          numbers = [int(c) for c in str(n)]
          new = 1
          for i in numbers:
              new *= i
          digits = len(str(new))
          return new, digits
      
      for n in xrange(99,9,-1):
          if persistence > 3:
              print 'persistence %i, number %i' % (persistence, n + 1)
              break
      
          digits, persistence = 2, 0
          while digits != 1:
              persistence += 1
              n, digits = newn(n)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-15
        • 2012-07-22
        • 1970-01-01
        • 2013-10-24
        • 1970-01-01
        • 2018-06-16
        相关资源
        最近更新 更多