Skip to content Skip to sidebar Skip to footer

Subsequence From Madhava–leibniz Series As Fast As Possible With Python

Madhava–Leibniz series: All I need is to create a subsequence from element k to element n: from this series to be created and returned as fast as possible. I started from 'l

Solution 1:

I slightly changed the madhava() function by using itertools.cycle:

defmadhava(k, n):  # Optimized for speed any way you like.# you don't need to allocate the array in advance, so comment it out# series = [0.0] * (n - k + 1)
    first_divisor = 2 * k + 1
    last_divisor_plus_1 = 2 * n + 2if k & 1:
        series = [what / divisor for what, divisor inzip(cycle([-1, 1]), range(first_divisor, last_divisor_plus_1, 2))]
    else:
        series = [what / divisor for what, divisor inzip(cycle([1, -1]), range(first_divisor, last_divisor_plus_1, 2))]
    return series

Original version on my machine (AMD 2400G, Ubuntu 18.04):

('3', '6', '7')
Linux-5.0.20-050020-generic-x86_64-with-Ubuntu-18.04-bionic
Python ('default', 'Oct 22 2018 11:32:17') GCC8.2.0Executingin 64bit

TimePi3.141592653589793Errorof calculation   function
m * n = 4 * 25 = 1000.0003.1315929035585540.00999975003123943 madhava
 0.0003.1315929035585540.00999975003123943 leibniz
m * n = 4 * 250 = 1,0000.0003.1405926538397940.000999999749998981 madhava
 0.0003.1405926538397940.000999999749998981 leibniz
m * n = 4 * 2,500 = 10,0000.0013.1414926535900349.99999997586265e-05 madhava
 0.0023.1414926535900349.99999997586265e-05 leibniz
m * n = 4 * 25,000 = 100,0000.0093.1415826535897201.0000000073340232e-05 madhava
 0.0163.1415826535897201.0000000073340232e-05 leibniz
m * n = 4 * 250,000 = 1,000,0000.0913.1415916535897741.0000000187915248e-06 madhava
 0.1503.1415916535897741.0000000187915248e-06 leibniz
m * n = 4 * 2,500,000 = 10,000,0000.8903.1415925535897921.0000000161269895e-07 madhava
 1.5463.1415925535897921.0000000161269895e-07 leibniz
m * n = 4 * 25,000,000 = 100,000,0009.0023.1415926435893261.0000467121074053e-08 madhava
15.6993.1415926435893261.0000467121074053e-08 leibniz

The version using itertools.cycle:

('3', '6', '7')
Linux-5.0.20-050020-generic-x86_64-with-Ubuntu-18.04-bionic
Python ('default', 'Oct 22 2018 11:32:17') GCC8.2.0Executingin 64bit

TimePi3.141592653589793Errorof calculation   function
m * n = 4 * 25 = 1000.0003.1315929035585540.00999975003123943 madhava
 0.0003.1315929035585540.00999975003123943 leibniz
m * n = 4 * 250 = 1,0000.0003.1405926538397940.000999999749998981 madhava
 0.0003.1405926538397940.000999999749998981 leibniz
m * n = 4 * 2,500 = 10,0000.0013.1414926535900349.99999997586265e-05 madhava
 0.0023.1414926535900349.99999997586265e-05 leibniz
m * n = 4 * 25,000 = 100,0000.0073.1415826535897201.0000000073340232e-05 madhava
 0.0163.1415826535897201.0000000073340232e-05 leibniz
m * n = 4 * 250,000 = 1,000,0000.0613.1415916535897741.0000000187915248e-06 madhava
 0.1523.1415916535897741.0000000187915248e-06 leibniz
m * n = 4 * 2,500,000 = 10,000,0000.6083.1415925535897921.0000000161269895e-07 madhava
 1.5303.1415925535897921.0000000161269895e-07 leibniz
m * n = 4 * 25,000,000 = 100,000,0006.1673.1415926435893261.0000467121074053e-08 madhava
15.6173.1415926435893261.0000467121074053e-08 leibniz

Another version, using itertools.starmap and operator.truediv:

defmadhava_leibniz_starmap(k, n):
    return starmap(truediv, zip(cycle([1, -1] if k & 1else [1, -1]), range(2*k+1, 2*n+2, 2)))

On my computer this yields:

('3', '6', '7')
Linux-5.0.20-050020-generic-x86_64-with-Ubuntu-18.04-bionic
Python ('default', 'Oct 22 2018 11:32:17') GCC8.2.0Executingin 64bit

chunks * elements = m * n = 4 * 25,000,000 = 100,000,000TimePi3.141592653589793Errorof calculation   function15.5693.1415926435893261.0000467121074053e-08 leibniz
 8.9683.1415926435893261.0000467121074053e-08 madhava
 6.1823.1415926435893261.0000467121074053e-08 madhava_leibniz
 5.2383.1415926435893261.0000467121074053e-08 madhava_leibniz_starmap

Which is roughly one second faster than list comprehension.

Post a Comment for "Subsequence From Madhava–leibniz Series As Fast As Possible With Python"