Skip to main content
explanation
Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87

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

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!

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

Source Link
ngn
  • 15.3k
  • 2
  • 43
  • 87

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!