Guest User

Untitled

a guest
Dec 12th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.18 KB | None | 0 0
  1. from math import sqrt; from itertools import count, islice
  2. import itertools
  3. from itertools import product
  4.  
  5.  
  6. #is n prime?
  7. def isPrime(n):
  8. #https://stackoverflow.com/questions/4114167/checking-if-a-number-is-a-prime-number-in-python
  9. return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
  10.  
  11. #find P(x) using the polyList to represent the polynomial
  12. def findSingleValue(polyList, x):
  13.  
  14. #https://stackoverflow.com/questions/18093509/how-can-i-create-functions-that-handle-polynomials
  15. return sum((a*x**i for i,a in enumerate(polyList)))
  16.  
  17.  
  18.  
  19. #is the polynomial prime for x <= p - 1?
  20. def isPolyPrime(polyList, prime):
  21. #polyValue = 0
  22. for x in range(prime - 1):
  23. polyValue = sum((a*x**i for i,a in enumerate(polyList)))
  24. if not isPrime(polyValue):
  25. return False
  26.  
  27. return True
  28.  
  29. #generate the next combo, given the previous combo
  30. def genCombo(combo, LB, HB):
  31. deg = len(combo)
  32. combo = list(combo)
  33. index = deg - 1
  34. while index >= 0:
  35. if combo[index] < HB:
  36. combo[index] += 1
  37. index = -1
  38. elif combo[index] == HB:
  39. combo[index] = LB
  40. index -= 1
  41. combo = tuple(combo)
  42. return combo
  43.  
  44.  
  45.  
  46. #main function
  47. def verifyPrime():
  48.  
  49. prime = int(input("Enter the prime number you want to find a poly for: "))
  50. LB = int(input("Enter the lower bound: "))
  51. HB = int(input("Enter the higher bound: "))
  52. deg = int(input("Enter the degree of the polynomial: "))
  53. lowDegPoly= input("Press n if you do not want to include lower degree polynomials: ")
  54.  
  55. allCombosNum = (abs(HB - LB))**deg - 1
  56.  
  57.  
  58.  
  59. #creates list of all possible tuples that represent a poly
  60.  
  61.  
  62. print("possible combos (including constant func): ")
  63. print(allCombosNum)
  64.  
  65. goodPolyList = []
  66.  
  67. combo = ()
  68.  
  69. #create the first combo - this is used as the basis to generate more combos
  70. for x in range(deg):
  71. combo += (LB,)
  72.  
  73.  
  74.  
  75. for x in range(allCombosNum):
  76. polyList = []
  77. polyList.append(prime)
  78. for coef in combo:
  79. polyList.append(coef)
  80. #now has a list of the prime and coefs; p + a1*x + a2*x^2 + ...
  81. isGoodPoly = isPolyPrime(polyList, prime)
  82. if isGoodPoly and not(lowDegPoly == "n" and combo[deg - 1] == 0):
  83. goodPolyList.append(polyList)
  84.  
  85.  
  86. #personal usage: keeps track of how many more combos it needs to go through
  87. numLeft = allCombosNum - x
  88. if (numLeft % 100000) == 0:
  89. print(numLeft)
  90.  
  91.  
  92. #create the next combo
  93. combo = genCombo(combo, LB, HB)
  94.  
  95.  
  96. print("################################################")
  97. print("poly generating finished")
  98. print()
  99. print(goodPolyList)
  100.  
  101. #bonus stuff
  102.  
  103. #goes over items in the goodPolyList and shows what primes each generates
  104.  
  105. for item in goodPolyList:
  106.  
  107. primeList = []
  108. for x in range(prime - 1):
  109. primeList.append(findSingleValue(item, x))
  110. print()
  111. print("List of primes that" , item, "generates: ")
  112. print(primeList)
  113.  
  114. print()
  115.  
  116. print("There are" , len(goodPolyList) , "good polynomials for", prime ,
  117. "with bounds" , LB , " to" , HB, "inclusive up to degree" , deg)
  118.  
  119. verifyPrime()
  120.  
  121. verifyPrime()
Add Comment
Please, Sign In to add comment