8
\$\begingroup\$

Given an integer N, output the Nth positive number K with the following property in decimal base:

For each digit I at position P of K, the number formed from K by removing the Pth digit (i.e. I) is divisible by I.

Example and remarks

324 is such a number:

  • 3 divides 24
  • 2 divides 34
  • 4 divides 32

Note 1: we assume that the empty number is divisible by anything, like 0. Therefore 1, 2, 3, 4, 5, 6, 7, 8 and 9 are valid.

Note 2: K cannot contain the digit 0, since you cannot divide by 0.

Inputs and outputs

  • You may take the input as a function argument, through STDIN, etc.
  • You may return the output from a function, through STDOUT, etc.
  • You may index those numbers starting from 0 (in which case N >= 0) or from 1 (in which case N > 0), whichever suits you more.

Test cases

Those examples are indexed from 0, so if you indexed from 1, then add 1 to the numbers in the N column.

N    Output
0    1
4    5
8    9
15   77
16   88
23   155
42   742
47   1113
121  4244
144  6888
164  9999

Scoring

This is , so the shortest answer in bytes wins.

\$\endgroup\$
4

9 Answers 9

4
\$\begingroup\$

Jelly, 21 19 bytes

DLR©œ^€®ịDḌḍ@DPȧµ#Ṫ

Input is 1-indexed. Try it online!

How it works

DLR©œ^€®ịDḌḍ@DPȧµ#Ṫ  Main link. No arguments.

                µ    Convert the chain to the left into a link.
                 #   Read n from STDIN and execute the link to the left for
                     k = 0, 1, 2, ... until n value return a truthy value.
D                      Convert D to base 10 (digit array).
 LR                    Construct the range from 1 to the length of the digit array.
   ©                   Copy the range to the register.
       ®               Yield the value of the register.
    œ^€                Take the multiset difference of each element of the range
                       and the range itself.
        ịD             Index into the digit array.
          Ḍ            Convert each list of digits to integer.
                       This yields all numbers with one suppressed digit.
           ḍ@D         Test each of these numbers for divisibility by the
                       suppressed digit from the digit array.
              P        Take the product of the resulting Booleans.
               ȧ       Logical AND with k (k would return 1).
                  Ṫ  Tail; extract the last (nth) element.
\$\endgroup\$
0
4
\$\begingroup\$

Pyth, 20

e.f!s.e.x%s.D`Zksb1`

Try it here or run the Test Suite

N is 1-indexed for this answer.

Uses largely the same logic as my python script. Notable differences are using .D to delete the digit from the string during the test and using exception handling to deal with zero digits.

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

Pyth, 26 bytes

e.f&!/J`Z\0!s%VsM.DLJUJjZT

Test suite.

e.f&!/J`Z\0!s%VsM.DLJUJjZT
e.f                          The n-th number (Z) where:
    !/J`Z\0                      Z does not contain "0"
   &                             and
           !s                    the following does not contain zero:
               sM.DLJUJ              generate the set of the digits removed
                       jZT           all the digits
             %V                      modulus in parallel
                                     if Z is 324,
                                     the first set would be [24,34,32]
                                     the second set would be [3,2,4]
                                     the third set would be [24%3, 34%2, 32%4]
\$\endgroup\$
0
2
\$\begingroup\$

Python 2, 93 bytes

n=input();r=0
while n:r+=1;k=t=1;exec't&=(r/k/10*k+r%k)%(r/k%10or r)<1;k*=10;'*r;n-=t
print r

Very inefficient. Input is 1-indexed. Test it on Ideone.

Alternate version, 100 bytes

The above code performs roughly 10x divisibility tests where only x are required. At the cost of only 7 extra bytes, efficiency can be improved dramatically, either by replacing *r with *len(`r`) or by refactoring the code as follows.

n=input();r=0
while n:
 r+=1;k=t=1
 for _ in`r`:t&=(r/k/10*k+r%k)%(r/k%10or r)<1;k*=10
 n-=t
print r

This handles all test cases with ease, even on Ideone.

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

JavaScript (ES6), 82 78 76 bytes

f=(n,k=0)=>n?f(n-!eval(/0/.test(++k)+`${k}`.replace(/./g,"|'$`$''%$&")),k):k

Input is 1-indexed. Works by building up a string of the form false|'24'%3|'34'%2|'32'%4 and evaluating it. Strings are used because they are still syntactically valid in the single-digit case.

The recursion limits this to about n=119. Iterative version for 88 84 82 bytes:

n=>{for(k=0;n;n-=!eval(/0/.test(++k)+`${k}`.replace(/./g,"|'$`$''%$&")));return l}

Edit: Saved 2 bytes thanks to @Dennis♦.

\$\endgroup\$
2
  • \$\begingroup\$ "|'$`$''%$&" saves two bytes. \$\endgroup\$
    – Dennis
    Commented Jun 14, 2016 at 23:12
  • \$\begingroup\$ @Dennis Thanks, I knew there had to be a better way to handle that edge case. \$\endgroup\$
    – Neil
    Commented Jun 14, 2016 at 23:49
2
\$\begingroup\$

Ruby, 90

f=->n,s=?0{0while s.next![?0]||s[1]&&/t/=~s.gsub(/\d/){eval"#$`#$'%#$&>0"};n<1?s:f[n-1,s]}

Similar approach to Neil's Javascript answer, but significantly longer due to a lack of implicit type conversion (except that booleans do get converted into strings by gsub, which is nice).

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

Ruby, 109 bytes

->n{i=s=1;n.times{s=?0;s="#{i+=1}"while s[?0]||(0...x=s.size).any?{|i|(s[0,i]+s[i+1,x]).to_i%s[i].to_i>0}};s}
\$\endgroup\$
1
\$\begingroup\$

Hoon, 246 bytes

=+
x=0
|=
n/@
=+
k=<x>
=+
%+
levy
%+
turn
(gulf 0 (dec (lent k)))
|=
a/@
=+
(trim +(a) k)
=+
(rash (snag a p) dit)
?:
=(- 0)
|
=(0 (mod (fall (rust (weld (scag a p) q) dem) x) -))
(curr test &)
?:
=(- &)
?:
=(0 n)
x
$(n (dec n), x +(x))
$(x +(x))

Ungolfed:

=+  x=0
|=  n/@
=+  k=<x>
=+  %+  levy
  %+  turn  (gulf 0 (dec (lent k)))
  |=  a/@
  =+  (trim +(a) k)
  =+  (rash (snag a p) dit)
  ?:  =(- 0)
    |
  =(0 (mod (fall (rust (weld (scag a p) q) dem) x) -))
(curr test &)
?:  =(- &)
  ?:  =(0 n)
    x
  $(n (dec n), x +(x))
$(x +(x))

This...is really terrible. I feel dirty for posting this.

Set k to the string form of the current number, map over the list [0...(length k)-1] splitting the string at that index (a). Get the a character, parse it to a number, and if it's 0 return no. Get the prefix of a and weld it to the other half of the split, parse to a number, check if the index divides it evenly.

++levy returns yes iff calling the function on all the elements of the list is also yes. In this case, the function ++test curried with yes, so it checks that all of the characters in k work.

If we're at the 0th value, we return the current number, or else we recurse with n decremented (and increment x)

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

Lua, 199 bytes

n,m=1,...function e(n)for i=1,#n do x=n:sub(1,i-1)..n:sub(i+1)s=n:sub(i,i)if x==""or s=="0"or x%s~=0 then return x==""end end return true end while m+0>0 do n=n+1 m=m-(e(""..n)and 1 or 0)end print(n)

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.