Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import permutations
- # Function 1 - isPrime
- def isPrime(n):
- if n == 1 or n == 2:
- return True
- for i in range(2, int(n/2)+1):
- if n % i == 0:
- return False
- return True
- # Function 2 - Create a list with all the permutations with input a given list
- def createPerms(nums):
- minSize = 2
- maxSize = len(nums)
- perms = []
- for size in range(minSize, maxSize+1):
- permsList = permutations(nums, size)
- for perm in permsList:
- perms.append(perm)
- return perms
- # Function 3 - Perms ----> Numbers
- def permsToNumbers(perms):
- numbers = []
- for perm in perms:
- number = 0
- for i in range(len(perm)):
- number += perm[i] * (10**(len(perm)-i-1))
- numbers.append(number)
- return numbers
- # Function 4 - Check if the list containers all all primes
- def checkIfAllPrimes(numbers):
- flag = True
- problem = 0
- for number in numbers:
- if isPrime(number) == False:
- flag = False
- problem = number
- break
- if flag:
- print("All the permutations of these numbers are primes!")
- else:
- print("There is at least one permutation of these numbers (" + str(problem) + "), that is not prime!")
- # MAIN FUNCTION
- 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]]
- for nums in numsList:
- print("Nums = " + str(nums))
- perms = createPerms(nums)
- numbers = permsToNumbers(perms)
- checkIfAllPrimes(numbers)
- print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement