Untitled

 avatar
unknown
python
3 months ago
1.1 kB
5
No Index
#!/usr/bin/python3

import gmpy2

def max_equal_consecutive_bit_count(n: gmpy2.mpz) -> tuple[int, int]:
    curr_consecutive_count = 0
    # max_0s_count = 0
    max_1s_count = 0

    prev_d = -1
    for i in range(n.bit_length()):
        d = n.bit_test(i)

        if d != prev_d:
            curr_consecutive_count = 1
            prev_d = d
        else:
            curr_consecutive_count += 1

        if d:
            max_1s_count = max(max_1s_count, curr_consecutive_count)
        # else:
        #     max_0s_count = max(max_0s_count, curr_consecutive_count)

    #return max_0s_count, max_1s_count
    return max_1s_count

if __name__ == "__main__":
    print("A070972, Length of longest run of consecutive 1's in binary expansion of 3^n, https://oeis.org/A070972")
    print()
    print("n\ta(n)")

    n_end_incl = 2000

    pow3 = gmpy2.mpz(1)
    for n in range(n_end_incl + 1):
        # max_0s_count, max_1s_count = max_equal_consecutive_bit_count(pow3)
        max_1s_count = max_equal_consecutive_bit_count(pow3)

        print(f"{n}\t{max_1s_count}")

        pow3 *= 3
Editor is loading...
Leave a Comment