20
\$\begingroup\$

A pandigital number is an integer which contains every digit from 0 to 9 at least once. 1234567890, 1902837465000000, and 9023289761326634265 are all pandigital. For the purposes of this challenge, numbers such as 123456789 are not pandigital, since they do not contain a 0, even though 123456789 = 0123456789.

A diverse pair of integers is a pair of integers \$(a, b)\$ such that \$a^b\$ is pandigital. \$b\$ is called the diversifying exponent.

Challenge: Given an integer \$a\$, find the smallest corresponding diversifying exponent \$b\$. This is a , so the shortest program in bytes wins.

(You may assume that there exists such an exponent, that is, your program will not be given invalid input, such as a power of 10.)

Your solution must be able to handle at the minimum the given test cases, but it should theoretically handle all valid inputs.

This is A090493 on OEIS.

Test cases

2 -> 68
3 -> 39
4 -> 34
5 -> 19
6 -> 20
7 -> 18
8 -> 28
9 -> 24
11 -> 23
12 -> 22
13 -> 22
14 -> 21
15 -> 12
16 -> 17
17 -> 14
18 -> 21
19 -> 17
20 -> 51
21 -> 17
22 -> 18
23 -> 14
24 -> 19
25 -> 11
26 -> 18
27 -> 13
28 -> 11
29 -> 12
30 -> 39
31 -> 11
32 -> 14
33 -> 16
34 -> 14
35 -> 19
36 -> 10
1234567890 -> 1
\$\endgroup\$
  • 3
    \$\begingroup\$ I want to point out a special case 1234567890 -> 1. \$\endgroup\$ – Bubbler Dec 20 '18 at 0:48
  • \$\begingroup\$ @Bubbler Added. \$\endgroup\$ – Conor O'Brien Dec 20 '18 at 3:03
  • \$\begingroup\$ are negative exponents off limits? \$\endgroup\$ – sudo rm -rf slash Dec 20 '18 at 11:51
  • 1
    \$\begingroup\$ Does something like 123456789 count as pandigital? It is equal to 0123456789, which is definitely pandigital. \$\endgroup\$ – wastl Dec 20 '18 at 18:08
  • 1
    \$\begingroup\$ @wastl no, it does not. \$\endgroup\$ – Conor O'Brien Dec 20 '18 at 19:17

20 Answers 20

9
\$\begingroup\$

Brachylog (v2), 9 bytes

;.≜^dl10∧

Try it online!

This is a function submission. The TIO link contains a wrapper that makes a function into a full program.

Explanation

;.≜^dl10∧
 .≜        Brute-force all integers, outputting the closest to 0
;  ^         for which {the input} to the power of the number
    d        has a list of unique digits
     l10     of length 10
        ∧  (turn off an unwanted implicit constraint)
\$\endgroup\$
7
\$\begingroup\$

Python 2, 44 bytes

f=lambda n,k=1:11>len(set(`k`))and-~f(n,n*k)

Input has to be a long, as ​`k`​ behaves differently for longs and ints.

Try it online!

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

Perl 6, 32 bytes

{first ($_** *).comb.Set>9,1..*}

Try it online!

Pretty self-explanatory.

Explanation

{                              }  # Anonymous code block
first                     ,1..*   # First positive number that
      ($_** *)    # When the input is raised to that power
              .comb.Set    # The set of digits
                       >9  # Is longer than 9
\$\endgroup\$
4
\$\begingroup\$

JavaScript (Node.js),  51 46  43 bytes

Takes input as a BigInt literal. Returns true instead of 1.

f=(n,k=n)=>new Set(n+'').size>9||1+f(n*k,k)

Try it online!

\$\endgroup\$
  • 2
    \$\begingroup\$ I keep forgetting JS has bigint's now :D \$\endgroup\$ – Conor O'Brien Dec 20 '18 at 0:44
  • \$\begingroup\$ I am slightly dubious regarding returning true instead of 1. That doesn't seem to match anything described at codegolf.meta.stackexchange.com/questions/9263/… \$\endgroup\$ – Sparr Dec 20 '18 at 1:43
  • 3
    \$\begingroup\$ @Sparr Here is the current consensus. \$\endgroup\$ – Arnauld Dec 20 '18 at 1:44
  • \$\begingroup\$ Thanks. I put a new Answer on my link referring to that. \$\endgroup\$ – Sparr Dec 20 '18 at 2:04
4
\$\begingroup\$

Ruby, 41 bytes

->n{i=0;i+=1until(n**i).digits.uniq[9];i}

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Haskell, 50 bytes

f a=until(\b->all(`elem`show(a^b))['0'..'9'])(+1)1

Try it online!

Same byte count:

f a=[b|b<-[1..],all(`elem`show(a^b))['0'..'9']]!!0
\$\endgroup\$
3
\$\begingroup\$

J, 25 bytes

>:@]^:(10>#@~.@":@^)^:_&1

Try it online!

Single monadic verb. The input should be an extended-precision integer (e.g. 2x).

How it works

>:@]^:(10>#@~.@":@^)^:_&1    Monadic verb. Input: base a
    ^:              ^:_      Good old do-while loop.
                       &1    Given 1 as the starting point for b,
>:@]                         increment it each step
      (            )         and continue while the condition is true:
               ":@^          Digits of a^b
            ~.@              Unique digits
          #@                 Count of unique digits
       10>                   is less than 10
\$\endgroup\$
  • \$\begingroup\$ (]+10>#@=@":@^)^:_* \$\endgroup\$ – FrownyFrog Dec 26 '18 at 13:18
2
\$\begingroup\$

Tcl, 82 bytes

proc X d {while {[llength [lsort -u [split [expr $d**[incr i]] ""]]]-10} {}
set i}

Try it online!

\$\endgroup\$
  • \$\begingroup\$ You can save some more bytes with llength 82 bytes \$\endgroup\$ – david Dec 20 '18 at 12:45
  • \$\begingroup\$ Saved some bytes, thenks to @david \$\endgroup\$ – sergiol Dec 20 '18 at 14:02
2
\$\begingroup\$

Racket, 110 96 bytes

-14 bytes thanks to UltimateHawk!

(define(f n[b 1])(if(= 10(length(remove-duplicates(string->list(~v(expt n b))))))b(f n(+ b 1))))

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ This can be shortened to 96 bytes by recursing on the function instead (define(f n[b 1])(if(= 10(length(remove-duplicates(string->list(~v(expt n b))))))b(f n(+ b 1)))) \$\endgroup\$ – Ultimate Hawk Dec 20 '18 at 14:23
  • \$\begingroup\$ @UltimateHawk Thank you! I forgot about the default parameters...(although the helper function also used default parameter b...) \$\endgroup\$ – Galen Ivanov Dec 20 '18 at 14:31
2
\$\begingroup\$

Python 3, 52 47 bytes

thanks to @BMO

f=lambda n,i=1:len({*str(n**i)})>9or 1+f(n,i+1)

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Just a heads up but you can just remove old code and put "<s>52</s> 47" in the header line. The edit log will retain the old versions if anyone is curious \$\endgroup\$ – Veskah Dec 20 '18 at 21:01
2
\$\begingroup\$

05AB1E (legacy), 10 9 bytes

Saved 1 byte thanks to Mr. Xcoder

XµINmÙgTQ

Try it online!

Explanation

Xµ           # find the first positive integer N that
  INm        # when the input is raised to N
     Ù       # and duplicate digits are removed
      g      # has a length
       TQ    # equal to 10
\$\endgroup\$
  • 1
    \$\begingroup\$ Legacy saves 1 byte: 1µINmÙgTQTry it online! \$\endgroup\$ – Mr. Xcoder Dec 20 '18 at 21:49
  • \$\begingroup\$ @Mr.Xcoder: Oh yeah, we had the implicit output of N then. Thanks! \$\endgroup\$ – Emigna Dec 20 '18 at 21:50
1
\$\begingroup\$

Charcoal, 19 bytes

WΦχ¬№IXIθLυIκ⊞υωILυ

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

WΦχ¬№IXIθLυIκ⊞υω

Repeatedly push the empty string to the empty list until there are no digits that the power of the input to the length of the list does not contain.

ILυ

Print the length of the list.

\$\endgroup\$
  • \$\begingroup\$ Why the downvote? \$\endgroup\$ – Luis Mendo Dec 20 '18 at 18:35
1
\$\begingroup\$

K (ngn/k), 76 bytes

{#{10>#?(+/|\0<|x)#x}{{+/2 99#,/|0 10\x,0}/+/99 99#,/a*\:x,0}\a::|(99#10)\x}

Try it online!

{ } function with argument x

|(99#10)\x we represent numbers as reversed lists of 99 decimal digits - do that to the argument

a:: assign to global variable a (k has no closures. we need a to be global so we can use it in subfunctions)

{ }{ }\ while the first function returns falsey, keep applying the second function (aka while loop), preserving intermediate results

a*\:x each of a's digits multiplied by each of x's digits ("outer product")

99 99#a*\:x,0 add an extra column of 0s and reshape again to 99x99, this shifts the i-th row by i items to the right, inserting 0s on the left (this works for the tests, for larger inputs 99x99 might lead to overflows)

+/ sum

{+/2 99#,/|0 10\x,0}/ propagate carry:

  • { }/ keep applying until convergence

  • 0 10\x divmod by 10 (a pair of lists)

  • |0 10\x moddiv by 10

  • 2 99#,/|0 10\x,0 moddiv by 10, with the "div" part shifted 1 digit to the right

  • +/ sum

{10>#?(+/|\0<|x)#x} - check for (not) pandigital:

  • |x reverse x

  • 0< which digits are non-zero

  • |\ partial maxima

  • +/ sum - this counts the number of leading 0s in x

  • 10> are they fewer than 10?

# length of the sequence of powers - this is the result

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

PowerShell, 107 bytes

param([bigint]$a)for([bigint]$b=1;-join("$([bigint]::pow($a,$b))"|% t*y|sort -u)-ne-join(0..9);$b=$b+1){}$b

Try it online!

Pretty straightforward, just a shame we need to use [bigint] everywhere. We take input $a, then setup a for loop with initializer $b=1.

Each iteration we increment $b after checking whether $a ^ $b (via pow) sent toCharArray, sorted with the -unique flag, then -joined together into a string is -notequal to the range 0..9 also -joined into a string.

That's a mouthful. For example, this would compare 7 ^ 5 = 16807 --> "01678" against "0123456789", determine they're not equal, and continue the loop.

Once we're out of the loop, we've determined which $b suits our input, and so leave that on the pipeline. Output is implicit.

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

Java, 108 bytes

a->{int b=0;while(new java.math.BigDecimal(a).pow(++b).toString().chars().distinct().count()<10);return b;};

Try it online!

Explanation

Brute force, looping a^b until it finds a string with 10 (or more, but that's impossible as there will only be 0 through 9) unique characters.

BigDecimal is required both because Math.pow is not accurate enough (fails on case 11), and also because converting a Double to a String by default shows scientific notation, which breaks this method of finding a pandigital number.

\$\endgroup\$
  • \$\begingroup\$ Don't Java vars start at 0 by default? Could save 2 bytes by eliminating the initialization. \$\endgroup\$ – Darrel Hoffman Dec 20 '18 at 22:13
  • \$\begingroup\$ @DarrelHoffman Instance variables do, yes. Locally-scoped variables do not. \$\endgroup\$ – Hypino Dec 20 '18 at 22:17
  • \$\begingroup\$ Ah, alright. Been some time since I worked in Java, forgot that technicality. \$\endgroup\$ – Darrel Hoffman Dec 21 '18 at 13:27
  • \$\begingroup\$ You can save 6 bytes by changing new java.math.BigDecimal(a).pow(++b).toString() to (new java.math.BigDecimal(a).pow(++b)+"") (and the trailing semi-colon doesn't have to be counted for lambda functions). Try it online \$\endgroup\$ – Kevin Cruijssen Dec 22 '18 at 16:39
1
\$\begingroup\$

Pyth, 10 8 bytes

fq;l{`^Q

Try it online here.

fq;l{`^QT   Implicit: Q=eval(input())
            Trailing T inferred
f           Return (and print) the first positive integer where the following is true:
      ^QT     Raise input to the current number-th power
     `        Convert to string
    {         Deduplicate
   l          Take the length
 q            Is the above equal to...
  ;           10

Saved 2 bytes thanks to FryAmTheEggman, previous code fq;l{j^QT;

\$\endgroup\$
  • \$\begingroup\$ You can use backtick to convert the number to a string instead of doing base conversion which will let you leave out the T in the power operation. \$\endgroup\$ – FryAmTheEggman Dec 20 '18 at 15:30
0
\$\begingroup\$

Jelly, 12 11 bytes

1*@ṾØDfƑʋ1#

Try it online!

How it works

1*@ṾØDfƑʋ1#  Main link. Argument: n

1            Set the return value to 1.
         1#  Call the link to the left for k = 1, 2, ... and with right argument n,
             until it returns a truthy value.
        ʋ      Combine the four links to the left into a dyadic chain.
 *@              Compute n**k.
   Ṿ             Convert it to its string representation.
    ØD           Yield "0123456789".
      fƑ         Filter and return 1 is the result is equal to the left argument.
\$\endgroup\$
0
\$\begingroup\$

Clean, 107 101 bytes

import StdEnv,Data.Integer
$a=hd[b\\b<-[1..]|length(removeDup[c\\c<-:toString(prod(repeatn b a))])>9]

Try it online!

Takes input as Integer, returns Int

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

Wolfram Language (Mathematica), 48 bytes

(For[n=1,!AllTrue[DigitCount[#^n],#>0&],n++];n)&

Try it online!

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

Attache, 27 bytes

${Generate{#Unique[x^_]>9}}

Try it online!

Explanation

${Generate{#Unique[x^_]>9}}
${                        }    lambda, input: x
  Generate{              }     first natural number _ satisfying...
                   x^_             the input to that number
            Unique[   ]          unique digits of ^
           #                   length of ^
                       >9      is greater than 9
                               i.e.: has 10 distinct digits

Alternatives

28 bytes: ${Generate{Unique@S[x^_]@9}}

29 bytes: ${Generate{Unique[S[x^_]]@9}}

30 bytes: ${Generate{#Unique[S[x^_]]>9}}

31 bytes: Generate@${{#Unique[S[x^_]]>9}}

32 bytes: ${Generate[{#Unique[S[x^_]]>9}]}

33 bytes: ${If[#Unique[x^y]>9,y,x&$!-~y]}&0

34 bytes: ${If[#Unique[x^y]>9,y,$[x,y+1]]}&0

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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