title: Can This Solution to the Google Foobar "Re-ID" be more efficient?
body: <p>I'm looking for guidance on a better way to solve Google's Re-ID problem. Instead of typing out the "Re-ID" description here, I will simply list the pertinent parts:</p>
  
  <ol>
  <li>There are 10,000 "Minions" each of which are assigned an integer randomly between 0 and 10000 inclusive.</li>
  <li><p>The Minion will look up his integer as an index in a string of concatenated prime numbers - i.e. <code>2357111317192329...</code></p></li>
  <li><p>His new "ID" is generated by finding the index value in the string according to the Minion's integer assignment and concatenating the next 4 numbers with it - i.e. if the Minion's integer assignment is <code>3</code> his new ID would be <code>71113</code> (<code>7</code> is the value at index 3 of the string, then we add the next 4 digits/characters)</p></li>
  </ol>
  
  <p>To solve this I:</p>
  
  <ol>
  <li>Utilized (thanks again, SO) a function to identify if a number is prime or not: <code>is_prime()</code></li>
  <li>Generated a string of all prime numbers concatenated together which facilitates a Minion with assignment "10000" plus the next 4 characters. </li>
  <li>Created a function to generate the new ID based on the Minion's assignment and the string of concatenated values: <code>answer()</code></li>
  </ol>
  
  <pre><code>import math
  def is_prime(n):
      if n % 2 == 0 and n &gt; 2: 
          return False
      return all(n % i for i in range(3, int(math.sqrt(n)) + 1, 2))
  
  primes = ''
  
  for i in range(2,21000,1):
      if len(primes) &lt; 10005:
          if is_prime(i):
              primes = primes + str(i)
      else:
          break
  
  def answer(n):
      re_id = primes[n:n+5:1]
      return(re_id)
  </code></pre>
  
  <p>Areas of possible improvement</p>
  
  <ol>
  <li>The <code>21000</code> in my for loop is completely arbitrary. By trial and error I found 21,000 facilitates a Minion integer assignment of 10000 plus the 5 additional digits/characters.</li>
  <li>Is it necessary to create this long string of concatenated prime numbers before hand? Could it not be done dynamically based on the Minion ID?</li>
  </ol>
  
  <p>Thanks for your insights. Happy Friday.</p>