Пример #1
0
def palindrome(B=10):
    """
    Palindrome Numbers: Non-negative integers that are palindromes in base B\n
    OEIS A002113, A006995
    """

    require_integers(["B"], [B])
    require_geq(["B"], [B], 2)

    if B == 10:
        for n in naturals():
            if str(n) == str(n)[::-1]:
                yield n

    else:
        for n in naturals():
            m = n
            d = []

            while n != 0:
                n, r = divmod(n, B)
                d.append(str(r))

            s = "".join(d)

            if s == s[::-1]:
                yield m
Пример #2
0
def collatz_length():
    """
    Collatz Sequence Lengths: Steps until the Collatz function equals 1 for each positive natural\n
    OEIS A006577, A008908
    """
    
    D = {1:0}
    
    for n in naturals(1):
        if n in D:
            yield D[n]
        
        else:
            ctr = n
            length = 0
            seen = []
            
            while ctr not in D:
                ctr = _collatz_step(ctr)
                length += 1
                seen.append(ctr)
            
            length = D[ctr] + length
            D[n] = length
            
            yield D[n]
            
            for num in seen:
                length -= 1
                D[num] = length
Пример #3
0
def perfect_powers():
    """
    Perfect Powers: Non-negative integers that can be written as a perfect power\n
    OEIS A001597 (when offset by 1)
    """

    yield 0
    yield 1

    for n in naturals(2):
        lim = floor(log2(n)) + 2

        for i in primes():
            ctr = 2

            while True:
                pwr = ctr**i

                if pwr > n:
                    break

                if pwr == n:
                    yield n
                    break

                ctr += 1

            if i > lim:
                break
Пример #4
0
def tribonnaci_partitions():
    """
    Unique representation of each natural as a sum of distrinct tribonacci numbers\n
    OEIS
    """
    
    trib = offset(tribonacci(),4)
    T = [1]
    
    yield (0,)
    
    for n in naturals(1):
        if T[-1] < n:
            T.append(next(trib))
        
        out = []
        
        for t in reversed(T):
            if n >= t:
                out.append(t)
                n -= t
            
            if n == 0:
                break
        
        yield tuple(out)
Пример #5
0
def collatz_highwater():
    """
    High-Water Mark of the Collatz Sequences: Record values for the high points of Collatz sequences\n
    OEIS A006885
    """
    
    D = {1 : 1}
    rec = 0
    
    yield 1
    
    for n in naturals(2):
        cur_rec = 0
        val = n
        
        while val not in D:
            cur_rec = max(cur_rec,val)
            val = _collatz_step(val)
        
        cur_rec = max(cur_rec,val)
        D[n] = max(D[val],cur_rec)
        
        if cur_rec > rec:
            yield D[n]
            rec = cur_rec
Пример #6
0
def tribonnaci_vectors():
    """
    Unique representation of each natural in the tribonacci base\n
    OEIS A278038
    """

    trib = offset(tribonacci(), 4)
    T = [1]

    yield (0, )

    for n in naturals(1):
        if T[-1] < n:
            T.append(next(trib))

        out = []

        for t in reversed(T):
            if n >= t:
                out.append(1)
                n -= t

            else:
                if 1 in out:
                    out.append(0)

        yield tuple(out)
Пример #7
0
def partition_count():
    """
    Partition Numbers: Number of unique multisets of positive integers with sum n\n
    OEIS A000041
    """
    
    D = [1]
    
    for n in naturals(1):
        yield D[-1]
        
        P = gen_pentagonal()
        next(P)
        
        sign = -1
        k=0
        
        for ctr,i in enumerate(P):
            if n-i < 0:
                D.append(k)
                break
            
            if ctr % 2 == 0:
                sign *= -1
            
            k += sign*D[n-i]
Пример #8
0
def totients():
    """
    Totients: Count of positive integers coprime to each positive integer\n
    OEIS A000010
    """
    
    D = defaultdict(list)
    
    yield 1
    
    for q in naturals(2):
        if q not in D:
            yield q-1
            D[q + q] = [q]
        
        else:
            n,d = 1,1
            
            for p in D[q]:
                D[p+q].append(p)
                
                n *= (p-1)
                d *= p
            
            yield q*n//d
            
            del D[q]
Пример #9
0
def primitive_pythagorean_triples():
    """
    All primitive pythagorean triples with hypotenuse in non-decreasing order and otherwise lexicographic order\n
    OEIS
    """

    triples = []

    for m in naturals(2):
        lim = m * m + 1

        for n in range(1 + m % 2, m, 2):
            if gcd(m, n) == 1:
                a = m * m - n * n
                b = 2 * m * n
                c = m * m + n * n

                triples.append((min(a, b), max(a, b), c))

        # This sorting assures that the hypotenuse is increasing and that
        # the if the hypotenuse is the same for two tuples the one with the
        # smaller initital term will go first
        triples.sort(key=lambda x: (x[2], x[0]))

        for i in triples:
            if i[2] > lim:
                break

            yield triples.pop(0)
Пример #10
0
def primitive_hypotenuse():
    """
    Primitive Hypotenuse Numbers: Positive integers that can be the hypotenuse of a primitive Pythagorean triple\n
    OEIS A008846
    """

    L = []

    for m in naturals(2):
        lim = m * m + 1  # smallest number that can be produced in this round

        for n in range(1 + m % 2, m, 2):
            if gcd(m, n) == 1:
                p = m * m + n * n

                if p not in L:
                    L.append(p)

        L.sort()

        for i in L:
            if i > lim:
                break

            yield L.pop(0)
Пример #11
0
def roman_numerals_str():
    """
    The positive integers as standard Roman Numerals, returns strings
    """

    for n in naturals(1):
        yield int_to_roman(n)
Пример #12
0
def leibniz_harmonic_triangle(flatten=False):
    """
    Leibniz's Harmonic Triangle: Each term is the sum of the two below\n
    
    Args:
        flatten -- boolean, if True returns one number at a time top to bottom left to right, otherwise returns on row at a time
    
    A003506
    """
    
    if flatten:
        for row in leibniz_harmonic_triangle(flatten=False):
            yield from row
    else:
        row = [1]
        
        for n in naturals(2):
            yield tuple(row)
            
            new_row = [Fraction(1,n)]
            
            for term in row:
                new_row.append(term-new_row[-1])
            
            row = new_row
Пример #13
0
def juggler_highwater():
    """
    High-Water Mark of the Juggler Sequences: Record values for the high points of Juggler sequences\n
    OEIS A143745
    """

    D = {1: 1}
    rec = 0

    yield 1

    for n in naturals(2):
        cur_rec = 0
        val = n

        while val not in D:
            cur_rec = max(cur_rec, val)
            val = _juggler_step(val)

        cur_rec = max(cur_rec, val)
        D[n] = max(D[val], cur_rec)

        if cur_rec > rec:
            yield D[n]
            rec = cur_rec
Пример #14
0
def wedderburn_etherington():
    """
    Wedderburn-Etherington Numbers\n
    OEIS A001190
    """
    
    S = [0,1,1]
    
    yield from S
    
    for n in naturals(2):
        # Odd step
        t = 0
        for i in range(1,n):
            t += S[i]*S[2*n-i-1]
        
        yield t
        S.append(t)
        
        # Even step
        t = (S[n]*(S[n]+1))//2
        for i in range(1,n):
            t += S[i]*S[2*n-i]
        
        yield t
        S.append(t)
Пример #15
0
def dyck_language():
    """
    All the Dyck words with 'up' as +1 and 'down' as -1
    """
    
    for n in naturals(1):
        yield from dyck_words(n)
Пример #16
0
def dyck_language_str():
    """
    All words consisting of correctly matched pairs of parentheses
    """
    
    for n in naturals(1):
        yield from dyck_words_str(n)
Пример #17
0
def juggler_length():
    """
    Juggler Sequence Lengths: Steps until the Juggler Sequence starting at each positive natural reaches 1\n
    OEIS A007320
    """

    D = {1: 0}

    for n in naturals(1):
        if n in D:
            yield D[n]

        else:
            ctr = n
            length = 0
            seen = []

            while ctr not in D:
                ctr = _juggler_step(ctr)
                length += 1
                seen.append(ctr)

            length = D[ctr] + length
            D[n] = length

            yield D[n]

            for num in seen:
                length -= 1
                D[num] = length
Пример #18
0
def prime_signatures():
    """
    Prime Signatures: The prime signature of each positive integer, returns tuples\n
    OEIS A124010
    """

    D = defaultdict(list)

    yield ()

    for q in naturals(2):
        if q not in D:
            yield (1, )
            D[q + q] = [q]

        else:
            T = []
            r = q

            for p in D[q]:
                T.append(0)
                D[p + q].append(p)

                while r % p == 0:
                    r //= p
                    T[-1] += 1

            yield tuple(T)

            del D[q]
Пример #19
0
def reflexive_relation():
    """
    Number of Reflexive Relations on n elements\n
    OEIS A053763
    """

    for n in naturals():
        yield 2**(n * n - n)
Пример #20
0
def sum_free_subset():
    """
    The number of sum-free subsets of the set {1...n} for all naturals n
    OEIS A007865
    """
    
    for n in naturals():
        yield len([i for i in sum_free_subsets(n)])
Пример #21
0
def central_binomial():
    """
    Central Binomial Coefficients: Middle term of the odd rows of Pascal's Triangle\n
    OEIS A000984
    """
    
    for n in naturals():
        yield comb(2*n,n)
Пример #22
0
def cake():
    """
    Cake Numbers: Maximum number of pieces produced when cutting a sphere with exactly n planes\n
    OEIS A000125
    """
    
    for n in naturals():
        yield (n*n*n+5*n+6)//6
Пример #23
0
def lazy_caterer():
    """
    Lazy Caterer Numbers: Maximum number of pieces produced when cutting a circle with exactly n lines\n
    OEIS A000124
    """
    
    for n in naturals():
        yield (n*(n+1))//2+1
Пример #24
0
def aliquot():
    """
    Aliquot Numbers: Sum of proper divisors for each positive integer\n
    OEIS A001065
    """
    
    for n in naturals(1):
        yield aliquot_sum(n)
Пример #25
0
def deficiency():
    """
    Deficiency: Each positive integer minus its aliquot sum\n
    OEIS A033879
    """
    
    for n in naturals(1):
        yield n-aliquot_sum(n)
Пример #26
0
def abundance():
    """
    Abundance: The aliquot sum of each positive number minus that number\n
    OEIS A033880
    """
    
    for n in naturals(1):
        yield aliquot_sum(n)-n
Пример #27
0
def relation():
    """
    Number of Binary Relations on n elements\n
    OEIS A002416
    """

    for n in naturals():
        yield 2**(n * n)
Пример #28
0
def combinadic(k):
    """
    k-Combinadic Numbers: Natural numbers represented as descending combinations of k elements\n
    OEIS
    """
    
    for n in naturals():
        yield int_to_comb(n,k)
Пример #29
0
def even_odd():
    """
    Positive integers but with odds and evens exchanged\n
    OEIS A103889
    """

    for n, s in zip(naturals(1), sign_sequence(1)):
        yield n + s
Пример #30
0
def all_quadratic_nonresidues():
    """
    Irregular array by rows listing all the quadratic nonresidues for each positive natural\n
    OEIS A096008
    """

    for n in naturals(1):
        yield from quadratic_nonresidue(n)