output_description: The output should be composed of lines each of which contains a single non-
  negative integer. It is the length of the prime gap that contains the
  corresponding positive integer in the input if it is a composite number, or 0
  otherwise. No other characters should occur in the output.
input_description: The input is a sequence of lines each of which contains a single positive
  integer. Each positive integer is greater than 1 and less than or equal to the
  100000th prime number, which is 1299709. The end of the input is indicated by
  a line containing a single zero.
attempt: #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  import math
  
  
  def sieve_of_erastosthenes(num):
      input_list = [
          False if i % 2 == 0 or i % 3 == 0 or i % 5 == 0 else True for i in range(num)
      ]
      input_list[0] = input_list[1] = False
      input_list[2] = input_list[3] = input_list[5] = True
      sqrt = math.sqrt(num)
  
      for serial in range(3, num, 2):
          if serial >= sqrt:
              return input_list
          for s in range(serial**2, num, serial):
              input_list[s] = False
  
  
  primeTable = sieve_of_erastosthenes(13 * 10**5)
  
  while True:
      k = int(input())
      if k == 0:
          break
      if primeTable[k]:
          print(0)
      else:
          i = 0
          while i < k or primeTable[i] == False:
              if primeTable[i]:
                  prevPrime = i
              i += 1
          print(i - prevPrime)
  
problem_description: B: Prime Gap
  
  The sequence of _n_ \- 1 consecutive composite numbers (positive integers that
  are not prime and not equal to 1) lying between two successive prime numbers
  _p_ and _p_ \+ _n_ is called a _prime_ gap of length _n_. For example, (24,
  25, 26, 27, 28) between 23 and 29 is a prime gap of length 6.
  
  Your mission is to write a program to calculate, for a given positive integer
  _k_ , the length of the prime gap that contains _k_. For convenience, the
  length is considered 0 in case no prime gap contains _k_.
samples: [{"input": "11\n    27\n    2\n    492170\n    0", "output": "0\n    6\n    0\n    114"}]