17
\$\begingroup\$

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...
\$\endgroup\$
7
  • 6
    \$\begingroup\$ 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? \$\endgroup\$
    – Wheat Wizard
    Feb 13, 2023 at 13:00
  • \$\begingroup\$ "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. \$\endgroup\$
    – Wyck
    Feb 16, 2023 at 19:37
  • \$\begingroup\$ @Wyck they have to be in order \$\endgroup\$
    – mousetail
    Feb 16, 2023 at 19:50
  • 1
    \$\begingroup\$ @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. \$\endgroup\$
    – Lynn
    Feb 20, 2023 at 21:49
  • \$\begingroup\$ @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. \$\endgroup\$
    – Wyck
    Feb 21, 2023 at 4:08

19 Answers 19

9
\$\begingroup\$

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.
    add edx, eax    # Add EAX to EDX.
    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.
\$\endgroup\$
1
  • \$\begingroup\$ bts sets a bit to 1, not 0. \$\endgroup\$ Feb 24, 2023 at 1:37
8
\$\begingroup\$

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!

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

05AB1E, 11 9 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).

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

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – c--
    Feb 13, 2023 at 21:42
  • 1
    \$\begingroup\$ Similarly 70 \$\endgroup\$
    – l4m2
    Jul 14, 2023 at 0:54
5
\$\begingroup\$

Vyxal, 11 10 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
\$\endgroup\$
4
  • \$\begingroup\$ Ninja'd! I was just about to post this 12 byte answer \$\endgroup\$
    – The Thonnu
    Feb 13, 2023 at 13:16
  • \$\begingroup\$ @TheThonnu and outgolfed too :p \$\endgroup\$
    – lyxal
    Feb 13, 2023 at 13:17
  • \$\begingroup\$ noob \$\endgroup\$
    – math scat
    Feb 13, 2023 at 13:25
  • 6
    \$\begingroup\$ 9 \$\endgroup\$
    – AndrovT
    Feb 13, 2023 at 13:41
4
\$\begingroup\$

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

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!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ +'0' can be +0 for -2. \$\endgroup\$ Feb 13, 2023 at 14:13
  • 2
    \$\begingroup\$ 59 bytes \$\endgroup\$
    – Arnauld
    Feb 13, 2023 at 14:28
3
\$\begingroup\$

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.

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

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

\$\endgroup\$
1
  • \$\begingroup\$ Most@DigitCount[n,#] -> DigitCount[n#,#] \$\endgroup\$
    – alephalpha
    Feb 13, 2023 at 15:09
3
\$\begingroup\$

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.

Explanation of older answer below:

⟨       ⟩#   # 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)
\$\endgroup\$
2
\$\begingroup\$

Java 8, 98 97 96 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().

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

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

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)

\$\endgroup\$
2
+200
\$\begingroup\$

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
\$\endgroup\$
4
  • \$\begingroup\$ yTDaPE0 can be y*aTDa for -1 (a tip I got from alephalpha on my 05AB1E answer). \$\endgroup\$ Feb 13, 2023 at 15:30
  • \$\begingroup\$ @KevinCruijssen nice, thanks! \$\endgroup\$
    – The Thonnu
    Feb 13, 2023 at 16:00
  • \$\begingroup\$ 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. \$\endgroup\$
    – DLosc
    Feb 17, 2023 at 22:49
  • \$\begingroup\$ As to y, it's initialized to "" (see the global variables list). In numeric contexts, that gets treated as zero. \$\endgroup\$
    – DLosc
    Feb 17, 2023 at 22:50
1
\$\begingroup\$

Charcoal, 22 bytes

NθNηWη«→≧⁻¬⁻…¹θ↨ⅈθη»Iⅈ

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:

NθNη

Input b and n as integers.

Wη«

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.

»Iⅈ

Output the final pandigital integer.

26 bytes to output the first n pandigital integers:

NθNηW‹Lυη«→F¬⁻…¹θ↨ⅈθ⊞υⅈ»Iυ

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.

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

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

Python, 93 90 87 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.

\$\endgroup\$
5
  • \$\begingroup\$ 90 \$\endgroup\$
    – The Thonnu
    Feb 13, 2023 at 19:46
  • \$\begingroup\$ 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. \$\endgroup\$
    – The Thonnu
    Feb 13, 2023 at 19:50
  • \$\begingroup\$ building on @TheThonnu, 87 \$\endgroup\$
    – c--
    Feb 13, 2023 at 21:55
  • \$\begingroup\$ 83 by merging f and g. \$\endgroup\$
    – user85795
    Feb 14, 2023 at 0:45
  • \$\begingroup\$ 82 with the same idea. \$\endgroup\$
    – user85795
    Feb 14, 2023 at 1:22
1
\$\begingroup\$

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

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!

\$\endgroup\$

Your Answer

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

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