10
\$\begingroup\$

Write a program or function that outputs all positive integers with distinct decimal digits (OEIS: A010784)

Examples:

1 2 3 4 5 6 7 8 9 10 12 13 ...
96 97 98 102 103 ...
123456789 123456798 ... 9876543120 9876543201 9876543210

Rules

  • You can output the list of numbers in any convenient format (string with separators, array, generator, ...)
  • The order of the numbers is not important as long as every number appears exactly once
  • You can choose if you include zero
  • This is the shortest solution wins
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Related: Determine if all decimal digits are unique \$\endgroup\$
    – xnor
    Sep 18 at 7:44
  • \$\begingroup\$ To clarify: should every number appear exactly once, or are repetitions allowed? \$\endgroup\$ Sep 18 at 16:37
  • \$\begingroup\$ @GregMartin "exactly one" means no repetitions (and that all numbers have to appear) \$\endgroup\$
    – bsoelch
    Sep 18 at 17:02

16 Answers 16

10
\$\begingroup\$

Haskell, 37 bytes

import Data.List
nub$nub.show<$>[1..]

Try it online!

Takes each number, removes duplicate digits from its string representation while preserving order, then removes duplicates from the resulting list.

41 bytes

[s|s<-show<$>[1..],s==[x|x<-s,y<-s,x==y]]

Try it online!

For both solutions, the resulting list is finite, but attempts to read further elements will not terminate. Adding an upper bound would fix this.

\$\endgroup\$
6
\$\begingroup\$

J, 14 bytes

~.~.@":&>i.!14

Attempt This Online! (with 7 instead of 14)

Uses xnor's idea: take unique digits from each number and then take unique results of that.


J, 18 bytes

I.(=&#=)@":&>i.!14

Attempt This Online! (with 7 instead of 14)

14! is the first factorial that exceeds 10^10.

I.(=&#=)@":&>i.!14
               !14    large enough number
             i.       all numbers up to that
           &>         for each number,
  (    )@":           stringify and check that,
      =               taking the unique chars
   =&#                keeps the length identical
I.                    output all true indices
\$\endgroup\$
2
  • \$\begingroup\$ Might it be shorter to take the unique chars of every number, then uniquify the list of results? \$\endgroup\$
    – xnor
    Sep 18 at 8:36
  • \$\begingroup\$ @xnor That's actually a good idea... \$\endgroup\$
    – Bubbler
    Sep 18 at 8:43
5
\$\begingroup\$

05AB1E, 5 4 bytes

∞€ÙÙ

Outputs an infinite list.

Try it online.

Explanation:

∞     # Push an infinite list of positive integers: [1,2,3,...]
 €Ù   # Uniquify the digits in each integer
   Ù  # Uniquify this infinite list
      # (after which this 'filtered' infinite list is output implicitly as result)
\$\endgroup\$
8
  • 1
    \$\begingroup\$ I'd be surprised if it really outputs an infinite list, bearing in mind that the list of numbers with distinct decimal digits is itself finite... \$\endgroup\$ Sep 18 at 8:16
  • \$\begingroup\$ @DominicvanEssen I guess you're partially right, but the same applies to most answers that results in an infinite output. The infinite list in 05AB1E is lazy, so it'll filter it, and then just time out after 60 seconds on TIO, while outputting as much as possible. And if you stop the execution of the program earlier, it'll still output the ones it has processed thus far. \$\endgroup\$ Sep 18 at 8:39
  • 2
    \$\begingroup\$ my point: programs must terminate by default... does this count as terminating? :P \$\endgroup\$ Sep 18 at 8:57
  • 1
    \$\begingroup\$ @KevinCruijssen it might count as outputting a generator or something though since you could (presumably) take this infinite list as input and output indices from it? either way it might be worth bringing up on meta before you go on an edit-spree, especially since nobody's seemed to have issue with it in the past. That being said please do save the old versions if you do edit, the results are still interesting even if they might not be considered "valid" for whatever reason... \$\endgroup\$ Sep 18 at 15:44
  • 1
    \$\begingroup\$ I would say this is valid, because, while we often assume answers are run in a theoretical interpreter that has infinite time an memory, nothing prevents us from instead using an actual interpreter with a timeout as our implementation instead. But it took me several rounds of thinking to reach that conclusion, so I totally understand where thejonymyster is coming from. \$\endgroup\$ Nov 19 at 1:14
4
\$\begingroup\$

K (ngn/k), 12 bytes

.'??'$!_1e10

Try it online! (with 1e3 instead of 1e10)

Uses xnor's idea: take unique digits from each number and then take unique results of that. Because the result is a list of strings, .' is necessary to convert them back to numbers.


K (ngn/k), 15 bytes

&~'/?:'\$!_1e10

Try it online! (with 1e3 instead of 1e10)

&~'/?:'\$!_1e10
         !_1e10    0..9999999999
        $          stringify each number
    ?:'\           iterate-collect over "uniquify each string"
                   which gives zeroth (original) and first (uniquified) iterations
 ~'/               1 if the two are equal, 0 otherwise for each item
&                  output nonzero indices
\$\endgroup\$
3
\$\begingroup\$

Vyxal, 26 bitsv2, 3.25 bytes

Þ∞'Þu

Outputs an infinite list.

Try it online.

Explanation:

Þ∞      # Push an infinite positive list: [1,2,3,...]
  '    '# Filter it by:
   Þu   #  All digits in the integer are unique
        # (after which this filtered infinite list is output implicitly as result)
\$\endgroup\$
3
\$\begingroup\$

Python 3, 49 bytes

n=1
while 1/n:10**len({*str(n)})>n!=print(n);n+=1

Try it online!

Uses my 10** trick from 2014. Converting the string representation of \$n\$ to a set removes duplicates, and we check whether this decreases the length by comparing to \$10^n\$, which is bigger than all \$n\$-digit number but no \$(n+1)\$-digit numbers.

The 1/n is used to theoretically halt for large enough n because doubles become zero around 1e-308 due to finite precision. If the program may just continue to run forever after printing all the numbers, this can be:

46 bytes

n=0
while[10**len({*str(n)})>n!=print(n)]:n+=1

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ How does this program choose if it calls print ? \$\endgroup\$
    – bsoelch
    Sep 18 at 8:05
  • \$\begingroup\$ @bsoelch It uses the fact that operator chains short-circuit and won't evaluate further than needed, see this tip. \$\endgroup\$
    – xnor
    Sep 18 at 8:07
3
\$\begingroup\$

JavaScript (SpiderMonkey), 43 bytes

for(i=99e9;i;)/(.).*\1/.test(--i)||print(i)

Try it online! (Linked version prints only up to 3 digits to avoid time out)

\$\endgroup\$
1
  • \$\begingroup\$ The SpiderMonkey link gives me a 404. Also, 1e10 will work as well—and it's a lot less performance-heavy. \$\endgroup\$
    – Someone
    Sep 23 at 18:54
3
\$\begingroup\$

Nekomata, 3 bytes

ŇƊů

Attempt This Online!

Outputs a list of lists of digits, without leading zeros. Zero is also output in this way (so appears in the output as an empty list).

Ň    # nondeterminiatically choose a natural number
 Ɗ   # convert it to a list of decimal digits
  ů  # keep them if they are all unique

Nekomata, 4 bytes

ŇƊůN

Attempt This Online!

Outputs a list of lists of digits, without leading zeros. Zero is not part of the output.
Works as the shorter answer above, but with an additional N to reject empty lists.

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

Brachylog, 3 bytes

ℕ≜≠

Try it online!

This is a generator, the output of this predicate unifies with each possible integer in increasing magnitudes. The try it online link has a header to unify the first 100 values.

Explanation

ℕ     A non-negative integer
 ≜    Pick one of them
  ≠   All elements (i.e. digits) must be different
\$\endgroup\$
2
\$\begingroup\$

Charcoal, 14 bytes

ΦI…⁰Xχχ¬⊙ι⊖№ιλ

Try it online! Link is to verbose version of code but only goes to 9876 because it would be too slow or run out of memory. Explanation:

  …             Range from
   ⁰            Literal integer `0`
     χ          Predefined variable 10
    X           Raised to power
      χ         Predefined variable 10
 I              Cast to string
Φ               Filtered where
         ι      Current string
       ¬⊙       No character satisfies
           №    Count of
             λ  Current character
            ι   In current string
          ⊖     Is not `1`
                Implicitly print
\$\endgroup\$
2
\$\begingroup\$

Retina 0.8.2, 27 bytes


9999999999$*¶

$.`
A`(.).*\1

Try it online! Link only goes to 9876 because it would be too slow or run out of memory. Explanation:


9999999999$*¶

$.`

List the numbers up to 9999999999.

A`(.).*\1

Delete those with duplicate digits.

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

Julia, 45 bytes

(r=1:2^34)[allunique.(r.|>x->"$x")].|>println

Attempt This Online!

Any number larger than 10^10 is a suitable upper limit for the range; using 2^34 saves a byte (this is reduces to 2^7 in the linked solution).

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

Ruby, 29 30 bytes

p (?0..?9*10).grep_v /(.).*\1/

Try it online!

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

Ruby, 47 bytes

f=->a=[*?1..?9],s=""{a.map{|i|f[a-[i],p(s+i)]}}

Try it online!

Link is a demo with only the digits 1..3

This recursively builds up the number from a pool of available digits. Longer than I had hoped for this approach, but efficient.

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

Bash, 34

seq -f%.0f 1e10|grep -Ev '(.).*\1'

If you have the jot command installed, you can save 1 byte by replacing the seq command with jot 9876543210.

This is quite slow - I think its been running 2 hours already and is 40% through.

Try it online!

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

Jelly, 7 bytes

ȷ10Q€ḌQ

Try it online!

A niladic link returning the full list of numbers. TIO link only returns values <1e5 since otherwise it runs out of memory (and would time out if not)

Uses the same methodology as @KevinCruijssen’s 05AB1E answer so upvote that one too!

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