Python optimizing code.
Hello, I started to learn python as a hobby a few months ago (meaning I am not an expert). I started to solve some kata on code wears. But I am stuck on two for the same reason " time out" meaning the server cannot run my cod in the time frame required and my code need some optimization. The trouble is I have no idea how to optimize this. I was asked to write a code, that give you the first pair of prime in a given interval (m and n) who are separated by a value (g) . For example in the frame (100, 110), 101 and 103 are the first two primes separated by 2. def step(g, m, n): r =[] for x in range(m,n+1): for k in range(3,x): if x%k == 0: break else: r.append(x) for x,y in enumerate(r[0:-1]): for y in r[x+1:-1]: if (y - r[x]) == g: return [r[x],y] Same with this one where you have to find the prim in an interval, where the reverse number is also a prime ( 9923 is prime and 3299 is also prime) def backwards_prime(start, stop): w =[] for x in range(start,stop+1): for y in range(2,x): if x%y == 0: break else: nx = int(str(x)[::-1]) for b in range(2,nx): if nx%b == 0: break else: w.append(x) return sorted(w) I do not see how to optimize those codes, any input appreciated.