Skip to content

Instantly share code, notes, and snippets.

@kg86
Created August 29, 2014 14:55
Show Gist options
  • Save kg86/0f53dccc4694cd7374ef to your computer and use it in GitHub Desktop.
Save kg86/0f53dccc4694cd7374ef to your computer and use it in GitHub Desktop.
import sys
 
M=[True] * 1000000
M[0] = M[1] = False
for i in xrange(2, len(M)):
  # print i
  if M[i]:
    j = i + i
    while j < len(M):
      M[j] = False
      j += i
 
def main(lines):
  d = {}
  for line in lines:
    d[int(line)] = 0
  count = 0
  for i in range(len(M)):
    if M[i]:
      count += 1
    if d.has_key(i):
      d[i] = count
  for line in lines:
    print d[int(line)]
 
if __name__=='__main__':
  lines = [x.strip() for x in sys.stdin.readlines()]
  for line in sys.stdin:
    lines.append(line.strip())
  # print lines
  main(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment