Computing persistence number of an integer

后端 未结 3 1014
难免孤独
难免孤独 2021-01-26 13:45

I am trying to make a code that does the following:

Multiplying the digits of an integer and continuing the process gives the surprising result that the

3条回答
  •  深忆病人
    2021-01-26 14:06

    You should use a while or for loop to multiply the digits instead of hardcoding what to do with the first, second and so on digits.

    In pseudocode...

    productSoFar = 1
    digitsLeftToMultipy = #the number
    while there are digits left to multiply:
        get the next digit and
        update produtsSoFar and digitsLeftToMultiply
    

    Also, use

    10 <= n < 100
    

    instead of

    n in range(10, 100)
    

    So you only do a couple of comparisons instead of a sequential lookup that takes time proportional to the length of the range.

提交回复
热议问题