小编Roh*_*ose的帖子

系列长度计算

我想计算下面算法给出的系列中的术语数:

if n=1 STOP:    
    if n is odd then n=3n+1 
    else n=n/2
Run Code Online (Sandbox Code Playgroud)

例如,如果n为22,则系列将如下所示,系列的长度将为16:

22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Run Code Online (Sandbox Code Playgroud)

我在python中想出了下面的递归函数:

class CycleSizeCalculator(object):
    '''
    Class to calculate the cycle count of a number. Contains the following
    members:
    counter - holds the cycle count
    number  - input number value
    calculate_cycle_size() - calculates the cycle count of the number given
    get_cycle_length()     - returns the cycle count
    '''
    def calculate_cycle_size(self,number):
        """
        Calculates the cycle …
Run Code Online (Sandbox Code Playgroud)

python recursion python-2.7

0
推荐指数
1
解决办法
347
查看次数

标签 统计

python ×1

python-2.7 ×1

recursion ×1