DEV Community

loading...
Cover image for Day 23: Ducci sequence

Day 23: Ducci sequence

mattryanmtl profile image Matt Ryan ・1 min read

A Ducci sequence is a sequence of n-tuples of integers, sometimes known as "the Diffy game", because it is based on sequences.

def ducci_sequence(*ns):
    while True:
        yield ns
        ns = tuple(abs(ns[i - 1] - ns[i]) for i in range(len(ns)))

def ducci(*ns):
    known = set()
    for ns in ducci_sequence(*ns):
        print(ns)
        if ns in known or set(ns) == {0}:
            break
        known.add(ns)
    return len(known) + 1
print(ducci(0, 345, 1788, 5078), "steps")
Enter fullscreen mode Exit fullscreen mode

Discussion (0)

Forem Open with the Forem app