예제 #1
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)
예제 #2
0
def odd_primes():
    """
    Odd Primes: Primes that are odd positive integer\n
    OEIS A065091
    """

    yield from offset(primes(), 1)
예제 #3
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)
예제 #4
0
def sparsely_totient():
    """
    Positive integers which have a totient less than all greater integers\n
    OEIS A036913
    """
    
    P = offset(primorial(),1)
    ctr = next(P)
    
    N = []
    T = []
    
    def find_sparse(N,T):
        for i,t in enumerate(T[:-1]):
            if t < min(T[i+1:]):
                yield N[i]
    
    for n,t in enumerate(totients(),1):
        N.append(n)
        T.append(t)
        
        if n == ctr:
            ctr = next(P)
            
            yield from find_sparse(N,T)
            yield n
            N = []
            T = []
예제 #5
0
def square_pyramidal():
    """
    Square Pyramidal Numbers: Positive integers that take the shape of a square based pyramid\n
    OEIS A000330
    """

    S = 1

    for s in offset(square(), 2):
        yield S
        S += s
예제 #6
0
def binary_length():
    """
    Binary Length: Bits in the binary representation of each non-negative integer\n
    OEIS A070939
    """

    yield 1
    yield 1

    for n, p in enumerate(offset(powers(2), 1), 2):
        for i in range(p):
            yield n
예제 #7
0
def ruler():
    """
    Ruler Sequence: Largest power of 2 dividing each positive integer, also height of gradation at each position of an infinite ruler\n
    OEIS A000120
    """

    for e in offset(evens(), 1):
        yield 0

        for n, p in enumerate(powers(2)):
            if e % p != 0:
                yield n - 1
                break
예제 #8
0
def alternating_factorials_1():
    """
    Alternating Factorial Numbers: Absolute value of each term\n
    OEIS A005165
    """

    cyc = sign_sequence(1)
    F = offset(factorials(), 1)
    out = 0

    for f, s in zip(F, cyc):
        yield abs(out)
        out += f * s
예제 #9
0
def semifibonacci():
    """
    The Semi-Fibonacci Sequence\n
    OEIS A030067
    """
    
    L = [1]
    
    for e in offset(evens(),1):
        yield L[-1]
        L.append(L[e//2-1])
        
        yield L[-1]
        L.append(L[e-1] + L[e-2])
예제 #10
0
def base_length(B=10):
    """
    Binary Length: Symbols in the representation of each non-negative integer in base B\n
    OEIS A070939
    """

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

    for i in range(B):
        yield 1

    for n, p in enumerate(offset(powers(B), 1), 2):
        for i in range(p):
            yield n
예제 #11
0
def wilson():
    """
    Wilson's Sequence: (n-1)! % n, for primes p-1, otherwise 0 except at four where it equals 2\n
    OEIS A061006
    """

    yield 0
    yield 1
    yield 2
    yield 2

    oldp = 5
    for p in offset(primes(), 2):
        for i in range(p - oldp - 1):
            yield 0

        yield p - 1
        oldp = p
예제 #12
0
def two_square():
    """
    Sums of exactly two non-zero squares\n
    OEIS A000404
    """

    sequence = offset(square(), 1)

    sqrs = []
    sums = []

    for s in sequence:
        sqrs.append(s)

        for i in sqrs:
            sums.append(i + s)

        sums = sorted(sums)

        while sums[0] <= s:
            yield sums.pop(0)
예제 #13
0
def totient_count():
    """
    Number of positive integers with totient n for positive n\n
    OEIS A014197
    """
    
    D = defaultdict(lambda:0)
    lo = 1
    
    S = offset(sparsely_totient(),1)
    ctr = next(S)
    
    for n,t in enumerate(totients(),1):
        D[t] += 1
        
        if n == ctr:
            ctr = next(S)
            
            for i in range(lo,t):
                yield D[i]
                del D[i]
            
            lo = t