Automatically find a suitable period to use.
        Factors are best, because they will have 1 left over when
        dividing SIZE+1.
        This only needs to be run once, on import.

def find_suitable_period():
    """ Automatically find a suitable period to use.
        Factors are best, because they will have 1 left over when
        dividing SIZE+1.
        This only needs to be run once, on import.
    """
    # The highest acceptable factor will be the square root of the size.
    highest_acceptable_factor = int(math.sqrt(SIZE))

    # Too high a factor (eg SIZE/2) and the interval is too small, too
    # low (eg 2) and the period is too small.
    # We would prefer it to be lower than the number of VALID_CHARS, but more
    # than say 4.
    starting_point = len(VALID_CHARS) > 14 and len(VALID_CHARS) / 2 or 13
    for p in range(starting_point, 7, -1) \
            + range(highest_acceptable_factor, starting_point + 1, -1) \
            + [6, 5, 4, 3, 2]:
        if SIZE % p == 0:
            return p
    raise Exception("No valid period could be found for SIZE=%d.\n"
                    "Try avoiding prime numbers" % SIZE)