In a private chat, a friend of mine apparently recently stumbled across a security system which has the following two restrictions on its valid pins:

  • Each digit must be unique (that is "1" may only appear once)
  • The order of the digits doesn't matter ("1234"="4321")

So to illustrate how bad this padlock system is, let's actually enumerate all valid PINs!


Your input will consist of a single, positive integer, which denotes the length of the PIN.


Your output consists of a list of non-negative integers or strings*, which enumerate all valid PINs of the given length.

*More precisely something that a human can use to try all combinations if you would print it for them. This means set of sets of digits and arrays of arrays of digits are fine.

Who wins?

This is so the shortest answer in bytes wins! Standard rules and loopholes apply.

Corner Cases

  • The output behaviour is undefined if an integer greater than 10 is input.
  • The ordering of the digits within each output entry is undefined, as entries with a zero actually contain said zero, that is you may not strip "0123" to "123" but "1230", "1203" and "1023" are all valid as is "0123".

Test Cases




  • 1
    The input is non-negative - so on input zero, the program should be a list containing one value, the empty string? – aschepler Jun 13 at 1:25
  • @aschepler indeed, the zero case is about as sensical as the 11 case, so I changed that to "positive integer". – SEJPM Jun 13 at 7:37

27 Answers 27

Jelly, 4 bytes


Try it online!


ØDœc  Double-builtin; main link
  œc  Number of combinations of          of length
ØD                              [digits]
                                                   <right argument>

Behavior for n > 10 is empty list.

  • how does it not work with input greater than 10 (tried it, and seemed weird to me)? – Windmill Cookies Jun 12 at 14:11
  • 2
    @gnu-nobody it gives the empty list because you can't have a combination of 11 elements with 10 choices. – HyperNeutrino Jun 12 at 14:12
  • oh, the ØD is the "0123456789". thanks. – Windmill Cookies Jun 12 at 14:13

05AB1E, 5 bytes


Try it online!


   sù  # keep elements the length of the input
  æ    # from the powerset
žh     # of 0123456789
  • Didn't know ù was a thing. Pretty sure I've used filter before. How new is that one? – Magic Octopus Urn Jun 12 at 17:57
  • 1
    @MagicOctopusUrn: Pretty old. Been there for a long time :P – Emigna Jun 12 at 19:44
  • Me brain no do see the command before the now. Me brain no am smart. – Magic Octopus Urn Jun 12 at 19:47
  • Has happened to me a couple of times. More than once on some commands as well, which is... – Emigna Jun 12 at 19:49
  • ... acceptable of a god-tier 05AB1Er (05AB1E-ite? 05AB1E-an?) such as yourself. – Magic Octopus Urn Jun 12 at 19:57

JavaScript (ES7), 89 bytes

Returns a list of lists of digits (as characters), or an empty list if n > 10.


Try it online!


We first generate a list of all decimal digits as characters by computing 229 = 536870912, adding the missing '4' and splitting:

[...2**29+'4'] → [ '5', '3', '6', '8', '7', '0', '9', '1', '2', '4' ]

We then compute the powerset:

.reduce(                                   ,[[]]) // starting with a[] holding an empty list
        (a,x)=>[                          ]       // for each decimal digit x:
                ...a,                             //   copy all previous entries in a[]
           >        )        //   and duplicate each previous entry y
                                 [x,...y]         //   with x prepended at the beginning

Finally, we filter the results on their length:


Python 3, 57 bytes

lambda l:combinations(range(10),l)
from itertools import*

Try it online!

Finds all combinations of 0 .. 9 of length l.

Behavior for n > 10 is empty list.

Wolfram Language (Mathematica), 22 bytes


Try it online!

Python 2, 62 bytes

f=lambda n:{d+s for d in`5**19`*n for s in f(n-1)if d>s}or{''}

Returns a set of strings.

Try it online!

Pyth, 4 bytes


Try it here


  UT     [0, 1, ..., 9].
.c  Q    All (implicit input)-element subsets.

R, 17 bytes


Try it online!

Errors for input greater than 10.

Returns a matrix where each column is a PIN.

Ruby, 30 bytes


Try it online!

MATL, 6 bytes


Try it online!

Returns nothing (empty array) for k>10.

          % implicit input k
4Y2       % push '0':'9'
   w      % swap top two elements of stack
    XN    % nchoosek, select all k-combinations of '0':'9' as a char array
          % implicit output

Haskell, 56 50 bytes

-6 bytes thanks to Hat Wizard.


Try it online!

Java (JDK 10), 105 bytes

n->{var s="";for(int i=1024,j;i-->0;s+=" ")for(j=10;n.bitCount(i)==n&j-->0;)s+=(1<<j&i)>0?j:"";return s;}

Try it online!

Haskell, 47 bytes

f 0=[[]]
f n=[a:x|x<-f$n-1,a<-[0..9],all(/=a)x]

Try it online!


When the number of digits is zero there is only one combination, that is the empty one:

f 0=[[]]

When the number of digits is n and n/=0 the combinations are all the ways to add digits to combinations from f$n-1 such that no digit is addded to a combination that already contains it.

f n=[a:x|x<-f$n-1,a<-[0..9],all(/=a)x]

Gaia,  4  3 bytes


Try it online!

It's been a while since I posted an answer in Gaia! Thanks to Mr. Xcoder for saving a byte!

      % implicit input n
₸     % push 10
 …    % pop 10, push 0..9
  K   % all subsets of size n
      % print top of stack implicitly
  • You don't need @. – Mr. Xcoder Jun 12 at 18:15
  • @Mr.Xcoder thanks. Didn't realize that; it doesn't seem to be documented very clearly. – Giuseppe Jun 12 at 18:17

Retina, 51 36 bytes


Try it online! Outputs nothing for n>10. Explanation:


Replace the input with 10 _s.


Repeat the rest of the program n times.


Prefix each number with _ repeated according to its first digit.


Match all _s, but include all of the following _s in the match too, for which we need to enable overlapping matches.


For each _ found, prefix the number of _s to its left to the number.

This is a bit tricky so perhaps an actual case would be better. Let's suppose that we've already run the loop twice, so that all 2-digit PINs have been generated, and we're currently working through them to create 3-digit PINs. We'll look at what happens to 36: The first digit is 3, so three _s are prefixed, to make ___36. This then creates the following matches, marked here with `'s:

Match   $%` $.%`
`___'36     0
_`__'36 _   1
__`_'36 __  2

$%' evalutes to 36 in all three cases, resulting in the 3-digit PINs 036, 136 and 236.

If we were then to go on to create 4-digit PINs, then 036 would not have any _s prefixed, and therefore would result in no matches at all in the final output.

  • The question says the input is non-negative. On input zero, this outputs ten underscore characters. – aschepler Jun 13 at 1:36
  • @aschepler Indeed; I see the OP has decided to exclude that case, but in case you're wondering, the fix would be to prepend ^0 and at a cost of 5 bytes. – Neil Jun 13 at 7:47

Proton, 43 bytes


Try it online!

Proton finally outgolfs Python :D I thought (import itertools) would return the value but apparently I failed at that. Also importing * afterwards doesn't work because it's not in a lambda, it's a top-level expression.

Japt, 5 bytes

Outputs an array of digit arrays. Outputs all combinations if input is 0 or an empty array if input is <0 or >10.

Ao àU

Try it


          :Implicit input of integer U
A         :10
 o        :Range [0,10)
   àU     :Combinations of length U
  • I don't think the output for input 0 is correct. The output should be a list containing one element, which is empty. – aschepler Jun 13 at 1:34
  • @aschepler, where are you getting that from? – Shaggy Jun 13 at 7:54

Stax, 4 bytes


Try it online!

Vd is "0123456789". , pushes the input to the main stack. S gets combinations of the specified size.

In the tio link, m is used in the footer to print each output.

Standard ML, 124 122 121 bytes

open List;fun f(s:: &)m=(if exists(fn x=>x=m)s then[]else[m::s])@f&m|f&m= &
fun!0=[[]]| !n=concat(tabulate(10,f(!(n-1))))

Try it online! Example usage: !2 yields [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[1,0],[1,2],[1,3], ...].


open List;

(* int list list -> int -> int list list *)
fun f (s::r) m =
    if exists (fn x => x=m) s 
    then f r m
    else (m::s) :: f r m
  | f [] m = []

(* int -> int list list *)
fun g 0 = [[]]
  | g n = concat(tabulate(10, f(g(n-1))))

Some alternatives:

125 123 bytes

fun f(s:: &)m=(if List.exists(fn x=>x=m)s then[]else[m::s])@f&m|f&m= &
fun!m 0=[[]]| !10n=[]| !m n=f(!0(n-1))m@ !(m+1)n;!0;

Try it online! Defines an anonymous function which is bound to it.

127 124 bytes

fun!0=[[]]| !n=let fun f(s:: &)m=(if List.exists(fn x=>x=m)s then[]else[m::s])@f&m|f&9=[]|f&m=f(!(n-1))(m+1)in f(!(n-1))0end

Try it online!

JavaScript (Node.js), 53 bytes


Try it online!

Oracle 18 SQL, 169 bytes

Not a golfing language but:

WITH t(v)AS(SELECT*FROM SYS.ODCINUMBERLIST(0,1,2,3,4,5,6,7,8,9)),n(a,b,c)AS(SELECT a,-1,''FROM i UNION ALL SELECT a-1,v,v||c FROM n,t WHERE v>b)SELECT c FROM n WHERE a=0

Expected the input to be in a table i with column a:


Try it online at Oracle Live SQL (a free login is required then copy-paste the solution into a worksheet) or SQLFiddle (no login but requires +7 bytes to work on the lower Oracle version).

CJam, 13 11 bytes


Try it online!

Technically doesn't run on, since heap space runs out. However, it works properly for up to 9 digit keypads, and should run just fine with more RAM.

Saved 2 bytes thanks to Dennis

Bash, 113 99 bytes

p()(r $1 0 $[10-$1])
r()for i in `seq $2 $3`;{ (($1>1))&&r $[$1-1] $[i+1] $[$3+1] $4$i||echo $4$i;}

Try it online!

  • r 0 doesn't work. – aschepler Jun 13 at 1:40
  • r is the recursive function: it's not meant to be launched with only 1 parameter. p 0 behaviour is unspecified in the question. – glenn jackman Jun 13 at 1:44

JavaScript (Firefox 30-57), 67 bytes

n=>n?[for(x of f(n-1))for(y of Array(x?+x[0]:10).keys())y+x]:['']

Port of my Retina answer, but works for n=0 too (returning a list of an empty string, as distinct from an empty list for n>10).

Charcoal, 21 bytes


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


Push the empty string to the predefined list.


Loop over each digit.


Append the digit to every string in the list.


Append the result to the original list.


Print all strings with the correct number of digits.

Perl 6, 20 bytes

{combinations 10,$_}

Try it online!

This is exactly what combinations (as a subroutine, or .combinations on a list) is for.

J, 32 bytes

.. frustratingly longer than Mathematica and R f=:{[:(#@>"0]/.])[:<@I.@#:@i.2^] TIO

Your Answer


By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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