접근

일단 문제를 보니 그냥 카운트를 하면 될 것 같아서 바로 코딩. 3번째 값도 그냥 별다른 어려움없이 바로 코드.
~cpp 
def countCode(n):
    count = 1
    while n != 1:
        if n % 2 == 1: n=n*3 + 1
        else: n = n/2
        count += 1
    return count

def cycleLengthMax(i,j):
    return max([countCode(each) for each in range(i,j+1)])

def cycleLengthMax2(i,j):
    print [(each,countCode(each)) for each in range(i,j+1)]

def cycleLengths(i,j):
    result = [countCode(each) for each in range(i,j+1)]
    result.sort()
    return result

def main():
    print countCode(22)
    print cycleLengthMax(1,10)
    print cycleLengthMax(100,200)
    print cycleLengthMax(201,210)
    print cycleLengthMax(900,1000)
    print cycleLengthMax(1,1000000)

def main2():
    print countCode(22)
    print cycleLengths(1,10)[-3]
    print cycleLengths(100,200)[-3]

def main3():
    for i in range(1,30):
        cycleLengthMax2(1,i)

if __name__=="__main__":
    import psyco
    psyco.full()
    #main()
    #main2()
    main3()
Retrieved from http://wiki.zeropage.org/wiki.php/3N+1Problem/1002
last modified 2021-02-07 05:22:16