DEV Community

Cover image for Day 23: Ducci sequence
Matt Ryan
Matt Ryan

Posted on

Day 23: Ducci sequence

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):
        if ns in known or set(ns) == {0}:
    return len(known) + 1
print(ducci(0, 345, 1788, 5078), "steps")
Enter fullscreen mode Exit fullscreen mode

Discussion (0)