def middle_square_weyl(n,k,m): """ Middle Square Method Augmented with a Weyl Sequence Args: n -- seed value k -- initial value for Weyl Sequence m -- modulus of Weyl Sequence """ # Only n has to be checked since the weyl() function will check k and m require_integers(["n"],[n]) W = weyl(k,m) N = int_to_digits(n) width = len(N)+(len(N)%2) for w in W: a = n*n+w A = int_to_digits(a) if len(A) % 2 == 1: A = [0] + A n = digits_to_int(A[width//2:-width//2]) yield n
def root_digits(n, a, B=10): """ Digits of the nth root of a in base B\n OEIS """ require_integers(["a", "n", "B"], [a, n, B]) require_geq(["a"], [a], 0) require_geq(["n", "B"], [n, B], 2) Bpow = B**n chunks = int_to_digits(a, Bpow) r, y = 0, 0 for d in chain(chunks, constant(0)): c = Bpow * r + d x = 0 for i in range(B): x += 1 if (B * y + x)**n - (Bpow * y**n) > c: x -= 1 break y1 = B * y + x r1 = c - ((B * y + x)**n - (Bpow * y**n)) r, y = r1, y1 yield x
def sqrt_digits(n, B=10): """ Digits of the square root of n in base B\n OEIS A002193, A002194, A002163, A010465, A010466, A010467, A010464, A010472, A010468, A010490, A010469, A010470, A010471, A010477, A010473, A010524, A010474 """ require_integers(["n", "B"], [n, B]) require_geq(["n"], [n], 0) require_geq(["B"], [B], 2) Bsq = B * B p, r = 0, 0 chunks = int_to_digits(n, Bsq) for d in chain(chunks, constant(0)): c = Bsq * r + d x = -1 for i in range(B): x += 1 if x * (x + (2 * B * p)) > c: x -= 1 break r = c - x * (x + 2 * B * p) p = B * p + x yield x
def stanley(): """ Stanley Numbers: Naturals that contain no 2s in their ternary expansion\n OEIS A005836 """ for n in naturals(): D = int_to_digits(n, 3) if 2 not in D: yield n
def cantor(): """ Cantor Numbers: Naturals that contain no 1s in their ternary expansion\n OEIS A005823 """ for n in naturals(): D = int_to_digits(n, 3) if 1 not in D: yield n
def champernowne_digits(B=10): """ Digits of the base B version of Champernowne's constant\n OEIS A003137, A030302, A030548, A033307, A030373, A031219, A030998, A031035, A031076 """ require_integers(["B"], [B]) require_geq(["B"], [B], 2) for n in naturals(1): yield from iter(int_to_digits(n, B))
def digit_including_words(d, B=10): """ Naturals that do contain the digit d in base B, returns tuples\n OEIS """ if d >= B: raise Exception("d must be less than B") for n in naturals(): D = int_to_digits(n, B) if d in D: yield D
def digit_avoiding_words(d, B=10): """ Naturals that do not contain the digit d in base B, returns tuples\n OEIS A005823, A005836 """ if d >= B: raise Exception("d must be less than B") for n in naturals(): D = int_to_digits(n, B) if d not in D: yield D
def radix_digits(B=10): """ The digits of each natural number in base B\n OEIS """ require_integers(["B"], [B]) require_geq(["B"], [B], 2) yield (0, ) for n in naturals(1): yield int_to_digits(n, B)
def middle_square(n): """ Middle Square Method Args: n -- seed value """ require_integers(["n"],[n]) N = int_to_digits(n) width = len(N)+(len(N)%2) while True: a = n*n A = int_to_digits(a) if len(A) % 2 == 1: A = [0] + A n = digits_to_int(A[width//2:-width//2]) yield n
def metallic_ratio_digits(n, B=10): """ Digits of the Nth Metallic Ratio in base B\n OEIS A001622, A014176, A098316, A098317, A098318, A176398, A176439, A176458, A176522 """ # Prepend zeroes to make addition line up dign = digits(n, B) digs = digits(isqrt(n * n + 4), B) if dign > digs: N = [0] * (dign - digs) + int_to_digits(n, B) elif dign < digs: N = [0] * (digs - dign) + int_to_digits(n, B) else: N = int_to_digits(n, B) # (n + √(n+4))/2 sq = sqrt_digits(n * n + 4, B) sm = real_sum(N, sq, B) M = real_div_nat(sm, 2, B) yield from dropwhile(lambda x: x == 0, M)
def sequence_including_words(S, B=10): """ Naturals that do include the sequence S as a subsequence of digits in base B, returns tuples """ for s in S: if s >= B: raise Exception("all values in S must be less than D") l = len(S) for n in naturals(): D = int_to_digits(n, B) d = len(D) if d < l: continue elif _subseq(S, D): yield D
def sequence_avoiding(S, B=10): """ Naturals that do not include the sequence S as a subsequence of digits in base B """ for s in S: if s >= B: raise Exception("all values in S must be less than D") l = len(S) for n in naturals(): D = int_to_digits(n, B) d = len(D) if d < l: yield n elif not _subseq(S, D): yield n
def quadratic_irrational(a, b, c, d, B=10): """ Digits of (a+b√c)/d """ require_integers(["a", "b", "c", "d", "B"], [a, b, c, d, B]) require_geq(["b", "c", "d"], [b, c, d], 1) require_geq(["B"], [B], 2) for i in naturals(2): if c % (i * i) == 0: raise Exception("c must be squarefree") if i * i > c: break diga = int_to_digits(a, B) sq = sqrt_digits(c, B) pr = real_prod_nat(sq, b, B) sm = real_sum(pr, diga, B) M = real_div_nat(sm, d, B) yield from dropwhile(lambda x: x == 0, M)