Advertisement
tthtlc

Project Euler Problem 49

Aug 14th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1.  
  2. import math
  3. import sys
  4.  
  5. def check_string_membership(i,j,k):
  6. istr=str(i)
  7. temp=[0 for x in range(0,26)]
  8. temp[int(istr[0])]+=1
  9. temp[int(istr[1])]+=1
  10. temp[int(istr[2])]+=1
  11. temp[int(istr[3])]+=1
  12. jstr=str(j)
  13. if (temp[int(jstr[0])]==0):
  14. return 0
  15. if (temp[int(jstr[1])]==0):
  16. return 0
  17. if (temp[int(jstr[2])]==0):
  18. return 0
  19. if (temp[int(jstr[3])]==0):
  20. return 0
  21. kstr=str(k)
  22. if (temp[int(kstr[0])]==0):
  23. return 0
  24. if (temp[int(kstr[1])]==0):
  25. return 0
  26. if (temp[int(kstr[2])]==0):
  27. return 0
  28. if (temp[int(kstr[3])]==0):
  29. return 0
  30. temp[int(jstr[0])]-=1
  31. temp[int(jstr[1])]-=1
  32. temp[int(jstr[2])]-=1
  33. temp[int(jstr[3])]-=1
  34. if (temp[int(jstr[0])]!=0):
  35. return 0
  36. if (temp[int(jstr[1])]!=0):
  37. return 0
  38. if (temp[int(jstr[2])]!=0):
  39. return 0
  40. if (temp[int(jstr[3])]!=0):
  41. return 0
  42. temp[int(istr[0])]+=1
  43. temp[int(istr[1])]+=1
  44. temp[int(istr[2])]+=1
  45. temp[int(istr[3])]+=1
  46. temp[int(kstr[0])]-=1
  47. temp[int(kstr[1])]-=1
  48. temp[int(kstr[2])]-=1
  49. temp[int(kstr[3])]-=1
  50. if (temp[int(kstr[0])]!=0):
  51. return 0
  52. if (temp[int(kstr[1])]!=0):
  53. return 0
  54. if (temp[int(kstr[2])]!=0):
  55. return 0
  56. if (temp[int(kstr[3])]!=0):
  57. return 0
  58. return 1
  59.  
  60. def erasthotene_cross(array, n):
  61. array = [0 for x in range(n)]
  62. for i in range(2,n,1):
  63. if (array[i]==0):
  64. for j in range(2*i, n, i):
  65. array[j]=1
  66. return array
  67.  
  68. max_nos=10000
  69. array=[]
  70.  
  71. array = [0 for x in range(max_nos)]
  72.  
  73. ### intersperse with zero...
  74. ### if array[p]==0,p>=2 then primer number
  75. print "cal prime nos"
  76. array=erasthotene_cross(array, max_nos)
  77.  
  78. print "calculate diff for all 4 digit prime"
  79. for i in range(1001,max_nos,2):
  80. if (array[i]==0):
  81. anchor=i
  82. for j in range(i+2,max_nos,2):
  83. if (array[j]==0):
  84. diff=j-i
  85. if (diff+j<max_nos):
  86. if (array[j+diff]==0):
  87. if (check_string_membership(i,j,j+diff)):
  88. print "found: %d %d %d"%(i, j, j+diff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement