I need to find a way of speeding up the following Python code, not by just using some kind of package, but by using a different method. I can only see that a for loop is going to work for a problem of this kind.
import time
start_time = time.clock() #Set the start time of the program to be the current time
Nlist = [10**2, 10**3, 10**4, 10**5, 10**6, 10**7, 10**8] #The values of N we are summing to
for k in range(len(Nlist)): #Repeating the loop for the amount of elements in our list
x = 0.
y = 0. #Set initial values for x and y
for i in range(1,Nlist[k]+1): #Summing over the various values of N (separately) in our list
x = 1./i #The summand
y += x #Keep adding 1/i for i=1,...,N i.e. our desired result
end_time = time.clock()
print('')
print('Time taken: {} seconds'.format(end_time - start_time)) #Prints time taken to run program
Here, I need to sum the harmonic series up to different values, namely N
. The above code takes around 25 seconds to execute.
Is there a better way of doing this?