12
\$\begingroup\$

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\$

Your task is to output this sequence.

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.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Nice first challenge! \$\endgroup\$
    – noodle man
    Feb 25 at 22:49
  • \$\begingroup\$ 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... \$\endgroup\$ Feb 26 at 4:23
  • \$\begingroup\$ @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. \$\endgroup\$
    – The Thonnu
    Feb 26 at 10:32
  • \$\begingroup\$ Potentially infinitely confused me, my bad. Shouldn't that be changed? \$\endgroup\$ Feb 26 at 13:08
  • \$\begingroup\$ @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 \$\endgroup\$
    – Bob th
    Feb 27 at 15:04

18 Answers 18

6
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 9 \$\endgroup\$
    – emanresu A
    Feb 25 at 21:28
3
\$\begingroup\$

Python 3, 55 bytes

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

Try it online!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
2
2
\$\begingroup\$

Retina 0.8.2, 72 bytes


1¶11¶111¶4$*
+%1`1
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.

+%1`1
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.

\$\endgroup\$
2
\$\begingroup\$

Wolfram Language (Mathematica), 64 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

Bash + bsdgames + coreutils, 40

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Perl 5, 59 bytes

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

Try it online!

\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

JavaScript (Node.js), 58 57 55 bytes

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

Try it online!

Port of Sisyphus's Python answer.

-3 thanks to @Arnauld

\$\endgroup\$
1
  • \$\begingroup\$ @Arnauld thanks, updated \$\endgroup\$
    – The Thonnu
    Feb 26 at 14:59
1
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

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?
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 29 bytes

IΦ×φχ∧⬤”)∨∧i#”‹№IιIμ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
                I           Cast to string
               ι            In outer value
              I             Cast to string
            ‹               Is less than
                   λ        Current character
                  I         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
I                           Cast to string
                            Implicitly print

34 bytes for a less inefficient version:

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

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

⊞υω

Start with the empty string.

Fυ

Loop over the strings of digits.

F⁺ι⁻⪪2357¹⪪ι¹

Append each prime digit that isn't yet present.

⊞υκ

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

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

Output only those strings that represent primes.

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

⊞υωF⁴«≔ΣE2357⁺κΦυ¬№μκυIΦIυ⬤…²κ﹪κμ

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

⊞υω

Start with the empty string.

F⁴«

Loop 4 times.

≔ΣE2357⁺κΦυ¬№μκυ

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

IΦIυ⬤…²κ﹪κμ

Output only those strings that represent primes.

\$\endgroup\$
0
\$\begingroup\$

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)
)
\$\endgroup\$
0
\$\begingroup\$

Factor,  64  45 bytes

[ "%5Iāȋवુಹ෇ᑵᒙ᱕ᵣ"nth ]

Try it online!

-19 by porting Sisyphus' Python answer.

Returns the nth number in the sequence.

\$\endgroup\$
0
\$\begingroup\$

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)

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.