# Prime combinations

Oct 27th, 2021
845
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. from itertools import permutations
2.
3. # Function 1 - isPrime
4. def isPrime(n):
5.     if n == 1 or n == 2:
6.         return True
7.     for i in range(2, int(n/2)+1):
8.         if n % i == 0:
9.             return False
10.     return True
11.
12. # Function 2 - Create a list with all the permutations with input a given list
13. def createPerms(nums):
14.     minSize = 2
15.     maxSize = len(nums)
16.     perms = []
17.     for size in range(minSize, maxSize+1):
18.         permsList = permutations(nums, size)
19.         for perm in permsList:
20.             perms.append(perm)
21.     return perms
22.
23. # Function 3 - Perms ----> Numbers
24. def permsToNumbers(perms):
25.     numbers = []
26.     for perm in perms:
27.         number = 0
28.         for i in range(len(perm)):
29.             number += perm[i] * (10**(len(perm)-i-1))
30.         numbers.append(number)
31.     return numbers
32.
33. # Function 4 - Check if the list containers all all primes
34. def checkIfAllPrimes(numbers):
35.     flag = True
36.     problem = 0
37.     for number in numbers:
38.         if isPrime(number) == False:
39.             flag = False
40.             problem = number
41.             break
42.     if flag:
43.         print("All the permutations of these numbers are primes!")
44.     else:
45.         print("There is at least one permutation of these numbers (" + str(problem) + "), that is not prime!")
46.
47. # MAIN FUNCTION
48. numsList = [ [1,3]  , [1,7] , [1,9], [3, 7], [3, 9], [7,9], [1,3,7], [1,3,9], [3,7,9], [1,3,7,9], [1,1,3], [1,1,7], [1,1,9], [1, 3, 3], [7, 7, 9], [1, 7, 7]]
49. for nums in numsList:
50.     print("Nums = " + str(nums))
51.     perms = createPerms(nums)
52.     numbers = permsToNumbers(perms)
53.     checkIfAllPrimes(numbers)
54.     print()
55.
56.
RAW Paste Data