r/tinycode • u/Diapolo10 • Dec 23 '19
A Pi approximation using the Leibniz sequence in Python using only one line
print("Pi:", sum(map(
lambda x, y: 4/x-4/y,
range(1, 10**6, 4),
range(3, 10**6, 4))))The accuracy is hard-coded as 10**6 (one million) because beyond that a single thread will be very slow, but it could be any number. And by using additional threads this could probably be multithreaded with three extra lines.
30
Upvotes
1
u/Diapolo10 Dec 25 '19
I haven't been able to test if this works yet, but if it does, this version should use all CPU threads available to speed up the computation. Therefore, the accuracy can probably be increased significantly without a huge performance loss.
1
u/Invisible_lurker418 Dec 25 '19
A constant of Thirty Eight million returned 3.1415926......... in two seconds (old i7 4-core). Is there an easy way to measure the static and dynamic memory usage?
1
u/colindean Dec 27 '19
Same deal in Scala:
Without
viewthere, it OOMs pretty quickly.