Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

/prog/ Challenge

Name: Anonymous 2007-09-04 15:06 ID:bG0CVxDp

Hey, I've stolen this challenge from some website, it's pretty easy but I know most of you will fuck it up.

A sequence is defined by:

n -> n/2 (n is even)
n -> 3n + 1 (n is odd)

Find the longest sequence, with a starting number under one million.

Bonus points when you find the solution for ten million.

Name: Anonymous 2007-09-05 4:13 ID:rABwucbx

memo = {}

def seq(n):
    m, i = n, 0
    while m != 1:
        if m in memo:
            i += memo[m]
            m = 1
        else:
            if m%2 == 0:
                m /= 2
            else:
                m = 3*m+1
            i += 1
    memo[n] = i
    return i

maximum, max_x = 0, None

for x in range(1, 10**6):
    length = seq(x)
    if length > maximum:
        maximum, max_x = length, x

print max_x


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List