This site is supported by donations to The OEIS Foundation.

User:R. J. Mathar/oeisPy/oeisPy/oeis bulk.py

From OeisWiki
Jump to: navigation, search
""" Native python3.1 implementations of OEIS http://oeis.org/classic functions.
        ==usage==
        python3
        >>> from oeisPy import *

        ==see also==
        http://www.jaapspies.nl/oeis for Jaap Spies' SAGE implementations.

        ==author==
        Richard J. Mathar, http://www.strw.leidenuniv.nl/~mathar/progs/oeisPy-0.0.1.tar.gz
"""

from math import *
from nzmath import *

def A001263(n, m):
        """
        AUTHOR: Richard J. Mathar 2009-10-11
        """
        return combinatorial.binomial(n-1, m-1)*combinatorial.binomial(n, m-1)//m

def A002024(n):
        """ Inverse triangular numbers.
        floor( (1+sqrt(1+8n))/2 ).

        ==input==
        n -- integer >=1.

        ==output==
        integer --

        ==examples==
        >>>oeis_bulk.A002024(3)
        3
        >>>oeis_bulk.A002024(4)
        4
        >>>oeis_bulk.A002024(5)
        4
        >>>oeis_bulk.A002024(28)
        7

        ==author==
        Richard J. Mathar 2010-09-06
        """
        a = 1
        nidx = 1
        while nidx < n:
                a += 1
                nidx += a
        return a

def A006519(n):
        """ Highest power of 2 dividing n
        This function returns 2^k where 2^k | n and k is maximum.

        ==input==
        n -- integer >=1.

        ==output==
        integer -- 1 if n is odd, else the associated power of 2.

        ==examples==
        >>>oeis_bulk.A006519(6)
        2
        >>>oeis_bulk.A006519(8)
        8
        >>>oeis_bulk.A006519(155)
        1

        ==author==
        Richard J. Mathar 2010-09-06
        """
        a = 1
        nshft = n
        while (nshft %2 == 0):
                a <<= 1
                nshft >>= 1
        return a

def A006519_list(n):
        """ List of highest powers of 2 dividing 1..n

        ==input==
        n -- integer >=1.

        ==output==
        list -- The first n values of A006519, starting [       1, 2, 1, 4, 1, 2, 1, 8, 1, 2]

        ==examples==
        >>>oeis_bulk.A006519_list(6)
        [1, 2, 1, 4, 1, 2]
        >>>oeis_bulk.A006519_list(8)
        [1, 2, 1, 4, 1, 2, 1, 8]

        ==author==
        Richard J. Mathar 2010-09-06
        """
        return [ A006519(i) for i in range (1,n+1)]
def A007814(n):
        """ Exponent of the highest power of 2 dividing n
        This function returns k where 2^k | n and k is maximum.

        ==input==
        n -- integer >=1.

        ==output==
        integer -- 0 if n is odd, else the associated exponent of 2.

        ==examples==
        >>>oeis_bulk.A007814(6)
        1
        >>>oeis_bulk.A007814(8)
        3
        >>>oeis_bulk.A007814(16)
        4
        >>>oeis_bulk.A007814(155)
        0

        ==author==
        Richard J. Mathar 2010-09-06
        """
        a = 0
        nshft = n
        while (nshft %2 == 0):
                a += 1
                nshft >>= 1
        return a

def A007814_list(n):
        """ List of exponents of largest powers of 2 dividing 1..n

        ==input==
        n -- integer >=1.

        ==output==
        list -- The first n values of A007814, starting [0,1,0,2,0,1,0,3]

        ==examples==
        >>>oeis_bulk.A007814_list(6)
        [0, 1, 0, 2, 0, 1]
        >>>oeis_bulk.A007814_list(8)
        [0, 1, 0, 2, 0, 1, 0, 3]

        ==author==
        Richard J. Mathar 2010-09-06
        """
        return [ A007814(i) for i in range (1,n+1)]

def A007953(n):
        """
        AUTHOR: Richard J. Mathar 2009-10-20
        """
        a=0
        sh=n
        while sh >0:
                a += sh % 10
                sh //= 10
        return a

def isA011540(n):
        """
        Code taken from the A071531 entry.
        AUTHOR: Tim Peters, 2005-05-19
        """
        while n:
                n, r = divmod(n, 10)
                if r == 0:
                        return True
        return False
def A071531(n):
        """
        AUTHOR: Tim Peters, 2005-05-19
        """
        r, p = 1, n
        while True:
                if isA011540(p):
                        return r
                r += 1
                p *= n


def A104711(n, m):
        """
        AUTHOR: Richard J. Mathar 2009-10-11
        """
        a =0
        for k in range(m, n+1):
                a += A001263(k, m)
        return a

def isA135027(n):
        """
        AUTHOR: Richard J. Mathar 2009-10-20
        """
        if n % 10 == 0:
                return False
        else:
                return A007953(n**2) == 10 ;

def A135027(n):
        """
        AUTHOR: Richard J. Mathar 2009-10-20
        """
        for n in range (0, 10000000):
                if isA135027(n):
                        print(n)

def A137932(n):
        """
        AUTHOR: William A. Tedeschi, 2008-02-29
        """
        return n**2 - (2*n- (n%2))

def A137935(n):
        """
        AUTHOR: William A. Tedeschi, 2008-03-06
        """
        return 5*n + 26*floor(n/5)

def A137936(n):
        """
        AUTHOR: William A. Tedeschi, 2008-03-06
        """
        return 5*(n%5) + floor(n/5)

def A147665(n):
        """
        AUTHOR: Richard J. Mathar 2009-10-18
        """
        if n <= 2:
                return [0,1,1][n]
        elif n % 3 <= 1:
                return A147665(A147665(n-1))+A147665(A147665(n//3))
        else:
                return A147665(A147665(n-1))+A147665(n-A147665(n//3))