-4

I like to explore this in terms of Asymptotic Notations: Big(O), Omega and Theta.

Here is a small piece of Python code. And tried running it by giving larger value each time. If you look at the 3rd scenario(image), the code is taking usually longer time to calculate the sum.

enter image description here

I wonder if i can rewrite it sequentially, would there be any difference? How can I optimize this code to take larger values? Thank you

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124
  • 1
    Well, 1e13 is a large number, it's normal the calculation takes a long time. You could take a look at this [page](https://en.wikipedia.org/wiki/Harmonic_number). You could return `0.5772156649 + math.log(n)` as an approximation. – Eric Duminil Feb 25 '18 at 15:30
  • Agreed! How can I optimize my code and make it better to take lesser time to process larger values. – Muhammad Maqsoodur Rehman Feb 25 '18 at 15:49
  • Possible duplicate: https://stackoverflow.com/questions/404346/python-program-to-calculate-harmonic-series – G_M Feb 25 '18 at 16:42

1 Answers1

0

You can make the same algorithm a lot faster by using numba:

from numba import jit

@jit
def compute(n):
    x = 0
    for i in range(1, n+1):
        x += 1/i
    return x

print(compute(1000000000))