Advertisement
Hydreigon_Lord

Frobenius Number

Jun 17th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. # Finding Frobenius Number of a Set
  2.  
  3. testSet = [6, 9, 20] # Set to test
  4.  
  5. def findGCF(s): # Finds greatest common factor of a list
  6.     factors = list(range(1,max(s)+1))
  7.     for i in s:
  8.         temp = []
  9.         for f in factors:
  10.             temp.append(f)
  11.         for k in temp:
  12.             if i%k!=0:
  13.                 factors.remove(k)
  14.     return max(factors)
  15.  
  16. print("These numbers can\'t be created with the numbers in your set:")
  17. gcf = findGCF(testSet)
  18. if gcf>1:
  19.     print("Any number that is not divisible by %s" % gcf)
  20.     for i in range(len(testSet)):
  21.         testSet[i]/=gcf
  22. consecPoss = 0
  23. num = 0
  24. impossNums = []
  25. while consecPoss < min(testSet):
  26.     impossible = True
  27.     num+=1
  28.     if num < min(testSet):
  29.         impossNums.append(num)
  30.     else:
  31.         for n in testSet:
  32.             if (num >= n) and ((num - n) not in impossNums):
  33.                 impossible = False
  34.         if impossible:
  35.             impossNums.append(num)
  36.             consecPoss = 0
  37.         else:
  38.             consecPoss += 1
  39. for i in impossNums:
  40.     printNum = i*gcf
  41.     print(printNum)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement