# Print all pandigital numbers

Given a base as input, output all pan-digital numbers. A number is pan-digital if it includes every digit in that base at least once, possibly multiple times. Every number is considered to contain an infinite number of leading 0s.

rules apply. Given a base, you may either:

• Given an index, output the n-th pan-digital number in that base
• Output all pan-digital numbers up to a given index, or
• Output all pan-digital numbers in that base in ascending order.

You may also submit a function that outputs a generator or infinite list.

## Test cases

2 -> 1, 2, 3, 4, 5, 6... (all natural numbers)
3 -> 5, 7, 11, 14, 15, 16, 17, 19...
4 -> 27, 30, 39, 45...
...
10 -> 123456789, 123456798...

• The sequence rules apply is a little confusing. You say they apply but then you skip options included for sequence in the default, such as outputing a generator or infinite list. Does the output need to be printed? Commented Feb 13, 2023 at 13:00
• "Output all pandigital numbers in that base infinitely" has a degenerate solution. Start with the last one. It begins with an infinite number of 1's. It cannot be shown to be a noncompliant implementation.
– Wyck
Commented Feb 16, 2023 at 19:37
• @Wyck they have to be in order Commented Feb 16, 2023 at 19:50
• @Wyck Regardless of the clarification mousetail offered, that's nonsense… If a program outputs nothing, it isn't a solution. There is also no natural number that "begins with an infinite number of 1s": natural numbers are finite. And in "it cannot be shown" you may be confusing the halting problem with the analysis of individual non-terminating programs, which is perfectly possible.
– lynn
Commented Feb 20, 2023 at 21:49
• @Lynn For any n, there is a pandigital number that begins what that many ones. My point was that if you chose one such number and started displaying it there's no way for a test to distinguish between being stuck in a loop and printing a compliant number that is larger than you expected. That's not a program that outputs nothing. I'm saying a golf program that outputs 1s cannot be demonstrated to be noncompliant via testing, only by static analysis. So it's better to just explicitly say "display all of them in ascending order" and then I'd have nothing to complain about.
– Wyck
Commented Feb 21, 2023 at 4:08

# x86-64 machine code, 28 bytes

31 C0 FF C0 50 31 C9 99 F7 FE 0F AB D1 01 C2 75 F6 58 FF C1 0F A3 F1 19 D7 73 E7 C3


Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes a 0-index in EDI and the base in ESI, and returns a number in EAX.

In assembly:

f:  xor eax, eax    # Set EAX to 0. EAX will hold the number being tested.
ol: inc eax         # Increase EAX by 1.
push rax        # Save the value of RAX (containing EAX) onto the stack.
xor ecx, ecx    # Set ECX to 0.
il: cdq             # Set EDX to 0 by sign-extending EAX.
idiv esi        # Divide EDX:EAX by ESI (the specified base).
# The quotient goes in EAX and the remainder goes in EDX.
bts ecx, edx    # Set the bit in ECX at position EDX (a digit) to 1.
jnz il          # Jump back if the result of the addition is nonzero.
# This loop will exit when the quotient and remainder are both zero.
# This performs one more iteration after EAX first becomes zero,
# thereby processing one leading zero and ending with EAX=0, EDX=0.
pop rax         # Restore the value of RAX (containing EAX) from the stack.
inc ecx         # Add 1 to ECX.
bt ecx, esi     # Set CF to the bit in ECX at position ESI (the base).
# If all digits are present, ECX was (2^base)-1 and became 2^base,
# and then CF=1. Otherwise, ECX is lower, and CF=0.
sbb edi, edx    # Subtract EDX+CF from EDI (thus -1 when EAX is pandigital).
jnc ol          # Jump back, to repeat, except when it goes from 0 to -1.
ret             # Return.

• bts sets a bit to 1, not 0. Commented Feb 24, 2023 at 1:37

# Python, 74 71 bytes

-3 thanks to pan

def f(b,i=1):b-len({i//b**k%b for k in range(i+1)})or print(i);f(b,i+1)


Attempt This Online!

• Welcome to Code Golf! Commented Feb 13, 2023 at 22:23
• Thanks, hope I'm doing everything right :) Commented Feb 13, 2023 at 22:24
• You can use a set comprehension for 71 bytes.
– pan
Commented Feb 15, 2023 at 2:36

# 05AB1E, 119 8 bytes

∞ʒ*IвÙgQ


-2 bytes thanks to @TheThonnu (¾šêIL<Q to 0šÙgQ).
-1 byte thanks to @alephalpha (Iв0š to *Iв).

Given $$\n\$$, outputs the infinite sequence.

Try it online.

Explanation:

∞         # Push an infinite positive list: [1,2,3,...]
ʒ        # Filter it by:
*       #  Multiply the current integer to the (implicit) input-integer
Iв     #  Convert it to base-input as list
Ù    #  Uniquify this list
g   #  Pop and push its length
Q  #  Check if it's equal to the (implicit) input-integer
# (after which the filtered infinite list is output implicitly)


Multiplying the integer by the input results in an additional trailing 0 in the base-converted list (thanks @alephalpha).

• 10 Commented Feb 13, 2023 at 14:57
• @TheThonnu Ah, of course. Thanks! And another byte can be saved by removing the second I for implicit input. :) Commented Feb 13, 2023 at 15:00
• 8 Commented Feb 13, 2023 at 15:07
• @alephalpha Oh, smart way to get rid of the prepend $0$! Thanks. Commented Feb 13, 2023 at 15:21

# JavaScript (Node.js), 64 bytes

Longer than EzioMercer's answer but works in theory1 for any base, rather than up to $$\36\$$.

Expects (base)(n) where both arguments are BigInts. Returns the $$\n\$$-th term of the sequence, 1-indexed.

b=>g=(k,q=x=1n,o=1n)=>q?g(k,q/b,o|1n<<q%b):k&&-~g(k-=-~o>>b,++x)


Try it online!

1: In practice, it's ironically worse because of the recursion.

### Without recursion, 70 bytes

-1 thanks to @l4m2

Backport of the C port.

b=>k=>{for(q=x=o=1n;k;q=q/b||(k-=-~o>>b,x+=o=1n))o|=1n<<q%b;return~-x}


Try it online!

# C (gcc), 66 bytes

-1 thanks to @ceilingcat
-1 thanks to @c--

A port of the JS version, but obviously without the benefit of BigInts.

o,q,x;f(b,k){for(q=x=0;k;q=q/b?:(k-=-~o>>b,x+=o=1))o|=1<<q%b;--x;}


Try it online!

• Suggest q/b?: instead of q?q/b: since q%b = 0 is contemplated with o=1, also possible is using !q?... and getting rid of the parentheses
– c--
Commented Feb 13, 2023 at 21:42
• Similarly 70
– l4m2
Commented Jul 14, 2023 at 0:54

# Vyxal, 1110 9 bytes

⁰τꜝ⁰ɽ⊍¬)ȯ


Try it Online!

Prints the first n pandigital numbers. Takes n then base.

-1 thanks to @mathcat

-1 thanks to @AndrovT

## Explained (old)

⁰τUs⁰ɽ~↔⁼)ȯ
)ȯ  # First n numbers where:
⁰τ           #   that number in base input
Us         #   uniquified sorted
⁰ɽ ↔     #   with digits from the range [1, input) kept
~ ⁼    #   equals that range

• Ninja'd! I was just about to post this 12 byte answer Commented Feb 13, 2023 at 13:16
• @TheThonnu and outgolfed too :p Commented Feb 13, 2023 at 13:17
• noob Commented Feb 13, 2023 at 13:25
• 9 Commented Feb 13, 2023 at 13:41

# Jelly, 7 bytes

1bŻĠṫʋ#


A dyadic Link that accepts the base on the left and the number of pan-digital numbers to find on the right and yields a list.

Try it online!

### How?

1bŻĠṫʋ# - Link: integer, B; integer, N
1       - set the left argument to one (k=1)
# - increment (k), yielding the first (N) for which:
ʋ  -   last four links as a dyad - f(k, B):   e.g. k=23, B=3
b      -     convert (k) to base (B)                   [2,1,2]
Ż     -     prefix with a zero                        [0,2,1,2]
Ġ    -     group indices by their values -           [[1],[3],[2,4]]
ṫ   -     tail (that) from index (B) -                      [[2,4]]
-       (the empty list is falsey, other lists are truthy)


# JavaScript (V8), 59 bytes

b=>{for(i=0;++i;)new Set(i.toString(b)+0).size<b||print(i)}


Try it online!

## UPD 62 -> 60

Thanks to Kevin Cruijssen for the tip to reduce bytes count

## UPD 60 -> 59

Thanks to Arnauld for the tip to reduce bytes count

# JavaScript (Node.js), 113 bytes

This code works for any base, rather than up to 36 and without a recursion

Expects one argument - b (base) as BigInt

b=>{for(n=0n;a=[],p=0n,k=++n;new Set(a).size<b||console.log(n)){while(k/b**p++);while(p--)k-=(a[p]=k/b**p)*b**p}}


Try it online!

• +'0' can be +0 for -2. Commented Feb 13, 2023 at 14:13
• 59 bytes Commented Feb 13, 2023 at 14:28

# PARI/GP, 46 bytes

n->for(i=1,oo,#Set(digits(i*n,n))<n||print(i))


Attempt This Online!

Saved some bytes by multiplying n instead of prepending 0.

# Wolfram Language (Mathematica), 47 bytes

If[DigitCount[n#,#]~FreeQ~0,Echo@n]~Do~{n,∞}&


Try it online! (press "play" to stop running)

prints sequence to infinity

-4 bytes from @alephalpha

• Most@DigitCount[n,#] -> DigitCount[n#,#] Commented Feb 13, 2023 at 15:09

# Gaia, 9 8 bytes

⟨×Bul=⟩#


Try it online!

Takes n, then b; returns the first n pandigital numbers in base b. Uses the same trick as this answer to add a zero.

⟨       ⟩#   # find the first n positive integers with a truthy result of:
B          # convert to base b (implicitly pushed)
0+        # add 0 to the list
u       # uniquify
l=     # is the length equal to the base (implicitly pushed again)

# Java 8, 989796 94 bytes

Given a base $$\b\$$, it'll output the infinite sequence separated by newlines (pretty similar to @EzioMercer's JavaScript answer):

b->{for(int i=0;;)if(b.toString(++i*b,b).chars().distinct().count()==b)System.out.println(i);}


Try it online.

Original answer (98 97 95 bytes): Given two inputs $$\b\$$ and $$\n\$$, outputs the $$\n^{th}\$$ term in base-$$\b\$$.

b->n->{int r=0,i,t;for(;n>0;)n-=b.toString(++r*b,b).chars().distinct().count()<b?0:1;return r;}


Try it online.

Explanation:

b->n->{                // Method with two integer parameters and integer return-type
int r=0;             //  Result-integer, starting at 0
for(;n>0;)           //  Loop as long as input n is not 0 yet:
n-=                //   Decrease n by:
b.toString(++r  //    Increase the result-integer by 1 first with ++r
*b   //    Multiply the new r by base b
//    (this is to get an additional trailing 0)
,b)  //    Convert r*b to base-b,
.chars()       //    Convert it to an IntStream of codepoint integers
.distinct()    //    Uniquify this IntStream
.count()       //    Get how many items are left
<b?             //    If this is smaller than base b:
0            //     Leave n the same by decreasing with 0
:             //    Else (the count is equal to b instead):
1;           //     Decrease n by 1
return r;}           //  After the loop, return the result-integer


Minor note: .distinct().count() is 4 bytes shorter than Java 16+'s .boxed().toSet().size().

# K (ngn/k), 16 bytes

{&x=#'?'+0,x\!y}


Try it online!

Takes the base as x and y as the zero-based index to generate all "pandigital" numbers up to.

• x\!y convert 0..y to base x
• +0, prepend a 0, then transpose to ensure all results contain a leading 0
• #'?' count the number of distinct digits in each value
• &x= return indices where all digits are present

# Julia 1.0, 49 bytes

>(b,n=1)=1:b-1⊆digits(n;base=b)&&@show(n),b>n+1


Try it online!

prints the sequence indefinitely. Uses the beautiful ⊆ (same as the function issubset)

# Pip, 29 24 23 20 bytes

W1{I#UQy*aTDa=aPyUy}


Attempt This Online!

-1 thanks to @KevinCruijssen and @alephalpha
-3 thanks to @DLosc

#### Explanation

W1{I#UQy*aTDa=aPyUy}  # Implicit input on command line
# y is automatically initialised to 0
# (in a numerical context)
W1{                }  # while True:
I                  #  if
#                 #   the length of
y*a            #   y * input
TDa         #   in base input
UQ               #   uniquified
=        #   equals
a       #   the input
Py     #  then print y
Uy   #  increment y


Old:

Y1W1{ISNUQyTDaPE0=,a{Py}Yy+1}  # Implicit input on command line
Y1                             # Set y to 1
W1{                       }  # while True:
I                         #  if
yTDa                 #   y in base input
PE0              #   with a 0 prepended,
SNUQ                     #   sorted and uniquified
=             #   equals
,a           #   range(0, input)
{  }       #  then
Py        #   print y
Yy+1   #  y = y + 1

• yTDaPE0 can be y*aTDa for -1 (a tip I got from alephalpha on my 05AB1E answer). Commented Feb 13, 2023 at 15:30
• @KevinCruijssen nice, thanks! Commented Feb 13, 2023 at 16:00
• A couple small tips: 1) If there's one statement in the body of an if or loop, you can drop the curly braces; 2) Uy works for ++y. Commented Feb 17, 2023 at 22:49
• As to y, it's initialized to "" (see the global variables list). In numeric contexts, that gets treated as zero. Commented Feb 17, 2023 at 22:50

# Charcoal, 22 bytes

ＮθＮηＷη«→≧⁻¬⁻…¹θ↨ⅈθη»Ｉⅈ


Try it online! Link is to verbose version of code. Takes b and n as input and outputs the nth pandigital number in base b. Explanation:

ＮθＮη


Input b and n as integers.

Ｗη«


Repeat until n is zero.

→


Move right one position. This uses the current column index as the next trial pandigital integer.

≧⁻¬⁻…¹θ↨ⅈθη


If the set difference between the range from 1 to b and the base conversion of the trial integer is empty then decrement n.

»Ｉⅈ


Output the final pandigital integer.

26 bytes to output the first n pandigital integers:

ＮθＮηＷ‹Ｌυη«→Ｆ¬⁻…¹θ↨ⅈθ⊞υⅈ»Ｉυ


Try it online! Link is to verbose version of code. Explanation: Pushes the pandigital numbers to the predefined empty list as they are found and outputs the final list once n pandigital numbers have been found.

# Pyth, 11 bytes

.f!-tUQjZQE


Try it online!

Prints the first n pandigital numbers, take input as base then n.

### Explanation

               # implicitly assign Q = eval(input())
E    # E = eval(input())  (second input)
.f        E    # print the first E numbers which satisfy lambda Z
jZQ     # convert Z to base Q as a list of digits
tUQ        # [1, 2,..., Q-1]
-           # remove all digits in our number from the list of all digits
!            # not (will be true for empty list)


# Python, 939087 82 bytes

-3 from @The Thonnu
-3 from @c--
-5 from @user85795

def f(b,i=1,j=1,*s):i and f(b,i//b,j,i%b,0,*s);b>len({*s})or print(j);f(b,j+1,j+1)


Attempt This Online! Infinitely prints out values.

• 90 Commented Feb 13, 2023 at 19:46
• Also, you might want to add something like import sys; sys.setrecursionlimit(10**9) in the header since at the moment it only prints values up to 1000. Commented Feb 13, 2023 at 19:50
• building on @TheThonnu, 87
– c--
Commented Feb 13, 2023 at 21:55
• 83 by merging f and g. Commented Feb 14, 2023 at 0:45
• 82 with the same idea. Commented Feb 14, 2023 at 1:22

# Japt, 14 bytes

0-indexed

o1
@eUfXìNÎ}iV


Try it

o1\n@eUfXìNÎ}iV     :Implicit input of integers U=base & V=index
o1                  :Range [1,U)
\n                :Reassign to U
@               :Function taking an integer X as argument
e              :  Test U for equality with
Uf            :    U filtered by elements of
Xì          :      X converted to digit array in base
N         :        Array of all inputs
Î        :        First element (i.e., original U)
}       :End function
iV     :Get the Vth integer that returns true


# C (clang), 65 bytes

j,k;f(*i,b,n){for(*i=j=0;n;j=!j?*i+=k=1:j%b==k?n-=++k/b,*i:j/b);}


Try it online!