input: Write a python function to find the largest prime factor of a given number.
  assert largest_prime_factor(13195) == 29
  assert largest_prime_factor(600851475143) == 6857
  assert largest_prime_factor(331) == 331
output: def largest_prime_factor(n):
      def is_prime(x):
          if x <= 1:
              return False
          if x <= 3:
              return True
          if x % 2 == 0 or x % 3 == 0:
              return False
          i = 5
          while i * i <= x:
              if x % i == 0 or x % (i + 2) == 0:
                  return False
              i += 6
          return True
      
      largest_prime = -1
      # Divide out the factor of 2
      while n % 2 == 0:
          largest_prime = 2
          n //= 2
      # Divide out other factors
      for i in range(3, int(n**0.5) + 1, 2):
          while n % i == 0:
              if is_prime(i):
                  largest_prime = i
              n //= i
      if n > 2 and is_prime(n):
          largest_prime = n
      return largest_prime