Discuss / Python / 利用到count, takewhile, cycle

利用到count, takewhile, cycle

Topic source

小喵biubiu

#1 Created at ... [Delete] [Delete and Lock User]

import itertools

def pi(N): OddNumber = itertools.count(1, 2) OddNumberN = itertools.takewhile(lambda x: x <= 2*N-1, OddNumber) p = [] ItCy = itertools.cycle([4, -4]) for ONN in OddNumberN: p.append(next(ItCy) / ONN) return sum(p)


  • 1

Reply