Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,796 answers

573 users

How to find the first 4-digit prime number where all digits are unique in Python

1 Answer

0 votes
import math

def is_prime(n: int) -> bool:
    if n < 2:
        return False
    if n % 2 == 0:
        return n == 2

    limit = int(math.sqrt(n))
    for i in range(3, limit + 1, 2):
        if n % i == 0:
            return False
    return True

def has_unique_digits(n: int) -> bool:
    digits = str(n)
    return len(set(digits)) == len(digits)

def main():
    for num in range(1000, 10000):
        if is_prime(num) and has_unique_digits(num):
            print(f"First 4-digit prime with all unique digits: {num}")
            return
    print("No such number found.")

if __name__ == "__main__":
    main()



'''
run:

First 4-digit prime with all unique digits: 1039

'''

 



answered Nov 20, 2025 by avibootz
...