# Primes with Distinct Prime Digits

There are 18 primes with distinct prime digits (A124674). Namely, they are:

$$\2, 3, 5, 7, 23, 37, 53, 73, 257, 523, 2357, 2753, 3257, 3527, 5237, 5273, 7253, 7523\$$

# Rules

• rules apply. This means valid solutions may use any of the following formats:

• Given some index $$\n\$$ it can return the $$\n\$$-th entry of the list.
• Given some index $$\n\$$ it can return all entries up to the $$\n\$$th one in the sequence.
• Without taking any index, it can output all entries by e.g. ...
• ...printing them one by one (potentially infinitely) or...
• ...returning a list (lazy if the sequence is infinite) or...
• ...returning a generator that represents the whole sequence.
• Note: the solution may print/generate infinitely, but once the entire sequence is output, subsequent outputs must be blank.
• If taken, you may assume the input $$\n\$$ is always valid. (with 0-based indexing, $$\ 0 \le n \le 17 \$$; with 1-based indexing, $$\ 1 \le n \le 18 \$$)

• This is ; fewest bytes wins.

• Standard loopholes apply.

• Nice first challenge! Feb 25 at 22:49
• We only have 10 digits in the base10 system (actually, less because this asks for prime digits) . I'm wondering how this sequence is infinite... Feb 26 at 4:23
• @UndoneStudios it's not infinite - the challenge says there are only 18 numbers in the sequence. The phrase "printing them one by one (potentially infinitely)" means that the program doesn't have to halt after printing all of them. Feb 26 at 10:32
• Potentially infinitely confused me, my bad. Shouldn't that be changed? Feb 26 at 13:08
• @UndoneStudios It's like that because it's copied from the sequence info page. However, I think it still applies, since my added note clarifies this issue Feb 27 at 15:04

# Jelly, 11 9 bytes

;DẒ;ŒQẠµ#


Try it online!

Takes $$\1 \le n \le 18\$$ on STDIN and returns the first $$\n\$$

## How it works

;DẒ;ŒQẠµ# - Main link. Takes no arguments
µ  - Previous chain as a monad f(k):
D        -   Digits of k, D
;         -   Prepnd k
Ẓ       -   Prime?
ŒQ    -   Unique sieve; Cast k to digits, replace a digit with a 1 if it hasn't appeared before, else 0
;      -   Concatenate the two lists
Ạ   -   All truthy?
# - Read n from STDIN. Starting k=0, count up until n k's return true under f(k)


The way ŒQ works can be shown e.g. with $$\k = 75523\$$:

   [7, 5, 5, 2, 3]
ŒQ:[1, 1, 0, 1, 1]


It auto-casts $$\k\$$ to digits, then replaces all but the first occurence of each element with 0.

# Vyxal, 12 11 9 bytes

Þp~Þu'fæA


Try it Online!

Prints the sequence infinitely.

Saved a byte by using a trick from caird's Jelly answer.
Saved 2 bytes thanks to emanresu A.

#### Explanation

Þp~Þu'fæA
Þp         # All primes
~Þu      # Filtered by are their digits unique
'fæA  # Filtered by are their digits all prime


Old:

Þun:fpæA∧)l
)l  # First n non-negative integers where:
Þu           #  It has unique digits
∧    #  And
n:fp       #  Its digits with it prepended
æA     #  All are prime

• 9 Feb 25 at 21:28

# Python 3, 55 bytes

lambda x:ord('%5Iāȋवુಹ෇ᑵᒙ᱕ᵣ'[x])


Try it online!

# 05AB1E, 14 11 10 bytes

∞ʒÐÙQiDªpP


Try it online!

Doesn't halt after printing all of them so you'll have to press the stop button.

-1 thanks to @KevinCruijssen

#### Explanation

∞ʒ          # All positive integers filtered by:
DÙQ       #  Digits are unique
i      # And:
Dª    #  Its digits with itself appended
pP  #  Are all prime

• You can combine the two filters with an if-statement for -1 byte: ∞ʒÐÙQiDªpP. :) Feb 26 at 18:50
• I've also posted an alternative 10-byter that does finish. ;) Feb 26 at 18:56

# Retina 0.8.2, 72 bytes


1¶11¶111¶4$* +%11 2$'¶$3$'¶$5$'¶$7 A(.).*\1 .+$*
A^(11+)\1+$%1  Try it online! Outputs all the terms of the sequence. Explanation:  1¶11¶111¶4$*


Insert 1, 2, 3, and 4 1s.

+%11
2$'¶$3$'¶$5$'¶$7


Expand into all possible integers with up to 4 prime digits.

A(.).*\1


Remove integers with duplicate digits.

.+
$*  Convert to unary. A^(11+)\1+$


Filter out composite numbers.

%1


Convert to decimal.

# Wolfram Language (Mathematica), 64 bytes

Select[FromDigits/@Join@@Permutations/@Subsets@{2,3,5,7},PrimeQ]


Try it online!

# Japt, 15 bytes

Èì_â fjÃ¶X©j}jU


Try it

Outputs the first $$\n\$$ entries in the sequence.

Explanation:

Èì_â fjÃ¶X©j}jU  # input stored in U
È           }jU  # get first U numbers where the following is true:
ì_              # convert to digits
â             #   keep unique digits
fj          #   remove non-prime digits
Ã¶X       # does this result in the same number?
©j     # and check the original is prime


# Bash + bsdgames + coreutils, 40

primes 2 7777|egrep -v '[^2357]|(.).*\1'


Try it online!

# Perl 5, 59 bytes

(1x$_)!~/^(11+)\1+$/&/^+\$/&!/(.).*\1/&&say for 2..1e4


Try it online!

# brainfuck, 213 bytes

-[----->+>+>+<<<]>-.---.>.<.>++.<.>++.<.>>-.+.<<.>>.<.<.>--.>.<<.>++.>.<<.>>-.<--.++.<.>--.>.+.<<.>>-.+.<.++.<.>>-.<.--.>+.<<.>>.-.<.++.<.>>+.<--.>-.<++.<.>--.>.+.<++.<.>--.>-.<++.>+.<<.>.>-.<--.>+.<<.>++.--.>-.+.


Try it online!

Sets up 3 cells with ASCII 255/5=51 (character 3) then produces the output hunt and peck style (with increments and decrements as necessary).

Cell 1 is used to print 2 and thereafter the separator /

Cell 2 is used to print 3 5 7 and thereafter the digits 5 & 7

Cell 3 is used to print the digits 2 and 3

It's possible that a 4th cell may lead to shorter code by allowing separate cells to be used for 5 and 7. I may investigate this later.

# JavaScript (V8), 66 bytes

Prints the sequence and keeps looping forever.

{for(n=x=2;;)n%--x||x>1|/(.).*\1|[^2357]/.test(x=n++)||print(n-1)}


Try it online!

# JavaScript (V8), 71 bytes

Prints the sequence and stops.

{for(n=x=2;n<8e3;)n%--x||x>1|/(.).*\1|[^2357]/.test(x=n++)||print(n-1)}


Try it online!

# JavaScript (Node.js), 58 57 55 bytes

n=>'%5Iāȋवુಹ෇ᑵᒙ᱕ᵣ'.charCodeAt(n)


Try it online!

-3 thanks to @Arnauld

• @Arnauld thanks, updated Feb 26 at 14:59

# 05AB1E, 10 bytes

₄ÅpʒÐÙSpÏQ


Outputs the entire sequence.

Try it online.

Alternative 10-byter that I deemed different enough for its own answer. Make sure to upvote @TheThonnu's 10-bytes 05AB1E answer as well!

Explanation:

₄           # Push 1000
Åp         # Pop and push a list of the first 1000 prime numbers (up to 7919)
ʒ        # Filter it by:
Ð       #  Triplicate the prime number
Ù      #  Pop one copy, and uniquify its digits
Sp    #  Check for each unique digit whether it's a prime number
Ï   #  Pop another copy, and only keep its digits at the truthy indices
Q  #  Check if the two integers are still the same
# (after which the filtered list is output implicitly)


# Stax, 15 bytes

¢\W░i*δÿ╛ñê╠≤▬J


Run and debug it

This is Packed Stax, which when unpacked represents the following 18 bytes

VIfEccu=^+_+{|pm|A


Run and debug it

VIf                # filter the following over infinity
E cu=           # Are all the digits in the number unique?
c   ^+         # Increment this and add it to the list (yielding prime value 2 for truthy)
_+       # add the current value to the list
|A # are all of the values in this list
{|pm   # prime?


# Charcoal, 29 bytes

ＩΦ×φχ∧⬤”)∨∧i#”‹№ＩιＩμＩλ⬤…²ι﹪ιλ


Try it online! Link is to verbose version of code. Explanation:

   φ                        Predefined variable 1000
×                         Multiplied by
χ                       Predefined variable 10
Φ                          Filtered where
”...”                Compressed string 1122121211
⬤                     All characters satisfy
№              Count of
μ          Inner index
Ｉ           Cast to string
ι            In outer value
Ｉ             Cast to string
‹               Is less than
λ        Current character
Ｉ         Cast to integer
∧                      Logical And
…      Range from
²     Literal integer 2
ι    To current value
⬤       All values satisfy
ι  Outer value
﹪   Modulo (i.e. is not divisible by)
λ Inner value
Ｉ                           Cast to string
Implicitly print


34 bytes for a less inefficient version:

⊞υωＦυＦ⁺ι⁻⪪2357¹⪪ι¹⊞υκＩΦＩυ∧ι⬤…²ι﹪ιλ


Try it online! Link is to verbose version of code. Explanation:

⊞υω


Ｆυ


Loop over the strings of digits.

Ｆ⁺ι⁻⪪2357¹⪪ι¹


Append each prime digit that isn't yet present.

⊞υκ


Save the new strings to the list of numbers with distinct prime digits.

ＩΦＩυ∧ι⬤…²ι﹪ιλ


Output only those strings that represent primes.

33 bytes by using the newer version of Charcoal on ATO:

⊞υωＦ⁴«≔ΣＥ2357⁺κΦυ¬№μκυＩΦＩυ⬤…²κ﹪κμ


Attempt This Online! Link is to verbose version of code. Explanation:

⊞υω


Ｆ⁴«


Loop 4 times.

≔ΣＥ2357⁺κΦυ¬№μκυ


For each prime digit, prefix it to all previous strings that do not already contain that digit, and concatenate all of the resulting lists.

ＩΦＩυ⬤…²κ﹪κμ


Output only those strings that represent primes.

# Excel, 145 bytes

=LET(
a,SEQUENCE(6^5),
b,FILTER(a,MMULT(N(LEN(a)-LEN(SUBSTITUTE(a,{2,3,5,7},""))=1),{1;1;1;1})=LEN(a)),
FILTER(b,MMULT(N(MOD(b,TOROW(a))=0),a^0)=2)
)


# Factor,  64  45 bytes

[ "%5Iāȋवુಹ෇ᑵᒙ᱕ᵣ"nth ]


Try it online!

-19 by porting Sisyphus' Python answer.

Returns the nth number in the sequence.

# SageMath, 288 bytes

Golfed version, run it online!

from itertools import permutations, chain, combinations; from sage.all import is_prime; print([int(''.join(map(str, p))) for p in chain.from_iterable(permutations(s) for s in chain.from_iterable(combinations({2, 3, 5, 7}, r) for r in range(1, 5))) if is_prime(int(''.join(map(str, p))))])


Ungolfed version, run it online!

from itertools import permutations, chain, combinations
from sage.all import is_prime

digits = {2, 3, 5, 7}
subsets = chain.from_iterable(combinations(digits, r) for r in range(1, len(digits) + 1))
perms = chain.from_iterable(permutations(s) for s in subsets)
nums = (int(''.join(map(str, p))) for p in perms)
primes = [n for n in nums if is_prime(n)]

print(primes)