Prime Numbers – N Digits

The program must accept N distinct digits as the input. The program must print all possible prime numbers (12 or 3 digits) that can be formed with these N digits. The prime numbers must be sorted.

Boundary Condition(s):
2 <= N <= 10

Input Format:
The first line contains N.
The second line contains N digits separated by a space.

Output Format:
The first line contains all possible prime numbers that can be formed with the given N digits.

Example Input/Output 1:
Input:
4
3 0 1 7

Output:
3 7 13 17 31 37 71 73 103 107 137 173 307 317 701

Explanation:
Here N=4 and the given 4 digits are 301 and 7.
All the possible prime numbers(containing unique digits) that can be formed with the digits 301 and 7 are given below
3 7 13 17 31 37 71 73 103 107 137 173 307 317 701

Example Input/Output 2:
Input:
5
1 0 2 5 8

Output:
2 5 251 281 521 821

def isprime(n):
    if n == 0 or n == 1:
        return False
    for i in range(2,n//2+1):
        if n % i == 0:
            return False
    return True
n = int(input())
lis = list(map(int,input().split()))
a = min(lis)
lis = sorted(lis,reverse = True)
s = ""
for i in lis:
    s += str(i)
maxnum = int(s)
for num in range(a,maxnum+1):
    numstr = str(num)
    if len(numstr) > 3:
        break
    flag = 1
    for ch in numstr :
        if ch not in s:
            flag = 0
        if numstr.count(ch) > 1:
            flag = 0
    if isprime(num) and flag == 1:
        print(num,end=" ")
Previous Article

Integers - Check Triplets

Next Article

Three Squares Encryption

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *