def bell(): """Bell or exponential numbers: number of ways to partition a set of n labeled elements. """ blist, b = [1], 1 yield 1 yield 1 while True: blist = list(itertools2.accumulate([b]+blist)) b = blist[-1] yield b
def is_perfect(n): """ :return: -1 if n is deficient, 0 if perfect, 1 if abundant :see: https://en.wikipedia.org/wiki/Perfect_number, https://en.wikipedia.org/wiki/Abundant_number, https://en.wikipedia.org/wiki/Deficient_number """ # return sign(abundance(n)) #simple, but might be slow for large n for s in itertools2.accumulate(divisors(n)): if s>2*n: return 1 return 0 if s==2*n else -1
def is_perfect(n): """ :return: -1 if n is deficient, 0 if perfect, 1 if abundant :see: https://en.wikipedia.org/wiki/Perfect_number, https://en.wikipedia.org/wiki/Abundant_number, https://en.wikipedia.org/wiki/Deficient_number """ # return sign(abundance(n)) #simple, but might be slow for large n for s in itertools2.accumulate(divisors(n)): if s > 2 * n: return 1 return 0 if s == 2 * n else -1
def accumulate(self, op=operator.add, init=[]): return Sequence(chain(init, itertools2.accumulate(self, op, False)))
def accumulate(self,op,skip_first=False): return Sequence(itertools2.accumulate(self,op,skip_first))
def accumulate(self,op=operator.add,skip_first=False): return Sequence(itertools2.accumulate(self,op,skip_first))