Thursday, November 19, 2009

Ducci Sequence in Python

A while back, I wrote this code in Python:
def myfunc(n):

    cur = [True] + [False for i in range(n-1)]
    hist = list()
    while cur not in hist:
        hist.append(cur)
        cur = [cur[i-1] ^ cur[i] for i in range(len(cur))]
    return (len(hist)-hist.index(cur))
It's kind of a long story how I came to be writing that particular snippet of code, but I did, and it outputs a sequence if you do this: print [myfunc(i) for i in range(1,20)]:
[1, 1, 3, 1, 15, 6, 7, 1, 63, 30, 341, 12, 819, 14, 15, 1, 255, 126, 9709]
I was wondering what kind of a sequence this might be, and in so wondering I became reacquainted with the On-Line Encyclopedia of Integer Sequences.  I put my sequence into the search box, et voila. I got the "Length of longest cycle for vectors of length n under the Ducci map."

As such, if you've come here for a quick and dirty way to calculate the longest cycle for vectors of length n under the Ducci map in Python, you're in the right place.  Unfortunately, I don't have an easy way to obscure this from the rest of you, but we'll be back to our irregularly occurring geekery some time arbitrarily far in the future.

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]