Advertisement
Guest User

Untitled

a guest
Oct 13th, 2013
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.84 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import sys
  3.  
  4. T=10 #no of test cases
  5. t=open(sys.argv[1],'r').readlines()
  6.  
  7. def sieve(maxNum):
  8.     yield 2
  9.     D, q = {}, 3
  10.     while q <= maxNum:
  11.         p = D.pop(q, 0)
  12.         if p:
  13.             x = q + p
  14.             while x in D: x += p
  15.             D[x] = p
  16.         else:
  17.             yield q
  18.             D[q*q] = 2*q
  19.         q += 2
  20.     raise StopIteration
  21.  
  22. def is_prime(n): # sieve of eratosthenes
  23.     ps, sieve = [], [True] * (n + 1)
  24.     for p in range(2, n + 1):
  25.         if sieve[p]:
  26.            ps.append(p)
  27.            for i in range(p * p, n + 1, p):
  28.                sieve[i] = False
  29.     return ps
  30.  
  31. #first line of each test case
  32. a=[1,4,7,10,13,16,19,22,25,28]
  33. count=0
  34. for i in a:
  35.  
  36.     b=t[i].split(" ")
  37.     c=b[1].split("\n")[0]
  38.     b=b[0]
  39.        
  40.     for k in xrange(int(b)):
  41.         d=t[i+1].split(" ")
  42.        
  43.         e=t[i+2].split(" ")
  44.         for g in d:
  45.             for j in e:
  46.                 try:
  47.                     sum=int(g)+int(j)
  48.                     p=is_prime(sum)        
  49.                     if p==True:
  50.                         count+=1
  51.                         print count
  52.                     else:
  53.                         pass
  54.                 except:
  55.                     try:
  56.                         g=g.strip("\n")
  57.                         sum=int(g)+int(j)
  58.                                             p=is_prime(sum)
  59.                                             if p==True:
  60.                                                     count+=1
  61.                                                     print count
  62.                                             else:
  63.                                                     pass
  64.                     except:
  65.                         j=j.strip("\n")
  66.                         sum=int(g)+int(j)
  67.                                             p=is_prime(sum)
  68.                                             if p==True:
  69.                                                     count+=1
  70.                                                     print count
  71.                                             else:
  72.                                                     pass
  73.  
  74.                
  75. print "Final count"+count
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement