Excludivisible numbers

8

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.

Fatalize

Posted 2016-06-14T06:36:22.113

Reputation: 32 976

Here are an implementation by @LeakyNun and by @FryAmTheEggman (both in Python) if you want more test cases.

– Fatalize – 2016-06-14T06:38:44.720

The first 10000 excludivisible numbers. – Leaky Nun – 2016-06-14T09:48:44.580

The first 100000 (1e5) excludivisible numbers. – Leaky Nun – 2016-06-14T09:59:20.960

Related. – AdmBorkBork – 2016-06-14T12:49:42.860

Answers

4

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.

Dennis

Posted 2016-06-14T06:36:22.113

Reputation: 196 637

4

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.

FryAmTheEggman

Posted 2016-06-14T06:36:22.113

Reputation: 16 206

2

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]

Leaky Nun

Posted 2016-06-14T06:36:22.113

Reputation: 45 011

2

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♦.

Neil

Posted 2016-06-14T06:36:22.113

Reputation: 95 035

"|'$`$''%$&" saves two bytes. – Dennis – 2016-06-14T23:12:00.760

@Dennis Thanks, I knew there had to be a better way to handle that edge case. – Neil – 2016-06-14T23:49:33.867

2

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.

Dennis

Posted 2016-06-14T06:36:22.113

Reputation: 196 637

2

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).

histocrat

Posted 2016-06-14T06:36:22.113

Reputation: 20 600

1

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}

Value Ink

Posted 2016-06-14T06:36:22.113

Reputation: 10 608

1

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)

RenderSettings

Posted 2016-06-14T06:36:22.113

Reputation: 620