Как оптимизировать код для нахождения простых чисел (Задачка из CodeWars)?
def isPrime(n):
 
    # Corner case
    if (n <= 1):
        return False
 
    # Check from 2 to sqrt(n)
    for i in range(2, int(n**.5)+1):
        if (n % i == 0):
            return False
 
    return True

def gap(distance, range_lower, range_upper):
  worked_area = list(range(range_lower,range_upper+1))

  find_primes = [el for el in worked_area if isPrime(el)]
  if len(find_primes) == 0:
    return []

  for i in range(1, len(find_primes)):
    if find_primes[i] - find_primes[i-1] == distance:
      return [find_primes[i-1], find_primes[i]]

  return []

Или:

def isPrime(n):
 
    # Corner case
    if (n <= 1):
        return False
 
    # Check from 2 to sqrt(n)
    for i in range(2, int(n**.5)+1):
        if (n % i == 0):
            return False
 
    return True

def gap(g, m, n):
    start = 0
    end = 0
    for i in range(m,n+1):
      if isPrime(i):
        if start == 0:
          start = i
        elif end == 0:
          end = i
        else:
          start = end
          end = i
      if end - start == g:
        return [start, end]
    return None
def isPrime(n):
 
    # Corner case
    if (n <= 1):
        return False
 
    # Check from 2 to sqrt(n)
    for i in range(2, int(n**.5)+1):
        if (n % i == 0):
            return False
 
    return True

def gap(distance, range_lower, range_upper):
  worked_area = list(range(range_lower,range_upper+1))

  find_primes = [el for el in worked_area if isPrime(el)]
  if len(find_primes) == 0:
    return []

  for i in range(1, len(find_primes)):
    if find_primes[i] - find_primes[i-1] == distance:
      return [find_primes[i-1], find_primes[i]]

  return []

Или:

def isPrime(n):
 
    # Corner case
    if (n <= 1):
        return False
 
    # Check from 2 to sqrt(n)
    for i in range(2, int(n**.5)+1):
        if (n % i == 0):
            return False
 
    return True

def gap(g, m, n):
    start = 0
    end = 0
    for i in range(m,n+1):
      if isPrime(i):
        if start == 0:
          start = i
        elif end == 0:
          end = i
        else:
          start = end
          end = i
      if end - start == g:
        return [start, end]
    return None