[Question]
  [
Starting with a positive integer **N**, find the smallest integer **N'** which can be computed by repeatedly dividing **N** by one of its digits (in base-10). Each selected digit must be a divisor of **N** greater than **1**.
## Example #1
The expected output for **N = 230** is **N' = 23**:
[![230/2=115, 115/5=23](https://i.stack.imgur.com/z1HEj.png)](https://i.stack.imgur.com/z1HEj.png)
## Example #2
The expected output for **N = 129528** is **N' = 257**:
[![129528/8=16191, 16191/9=1799, 1799/7=257](https://i.stack.imgur.com/q4JDw.png)](https://i.stack.imgur.com/q4JDw.png)
Beware of non-optimal paths!
We could start with **129528 / 9 = 14392**, but that would not lead to the *smallest* possible result. The best we can do if we first divide by 9 is:
[![129528/9=14392, 14392/2=7196, 7196/7=1028, 1028/2=514 --> wrong!](https://i.stack.imgur.com/dkp8M.png)](https://i.stack.imgur.com/dkp8M.png)
## Rules
* Input can be taken in any reasonable format (integer, string, array of digits, ...).
* This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest answer in bytes wins!
## Test cases
```
1         --> 1
7         --> 1
10        --> 10
24        --> 1
230       --> 23
234       --> 78
10800     --> 1
10801     --> 10801
50976     --> 118
129500    --> 37
129528    --> 257
8377128   --> 38783
655294464 --> 1111
```
  
[Answer]
# [Haskell](https://www.haskell.org/), ~~67~~ 61 bytes
```
f n=minimum$n:[f$div n d|d<-read.pure<$>show n,d>1,mod n d<1]
```
[Try it online!](https://tio.run/##XU9BboMwELz7FXvg0Ep25LUNNhXhB31BlAOKobGCHQRNe2j79VKHhhTqw65mNJ6dOVbDqW7bcWwgbL0Lzl98Ep52TWLdGwSwn7ZgfV3ZTXfp6yIph@P5HQK1JVJ/tldFgfvRVy7AFnzVPcND17vwChtoHmGHVFPkVCgqZFxSRWQ4nybSlOc6oyjy9ErFJQw1UmuMO0tTkSuVqT35YARhfoyVgET/w8hXmBOh1oJ4foGFjIRaENqQKdjKMkZcWEZEpsB/HMZfU/iZkZr81rgfSjW5NZolRhtJ7u1uRoiEfY3fh6atXoaRHbruBw "Haskell – Try It Online")
### Explanation:
* `read.pure<$>show n` transforms the input integer `n` into a list of digits.
* For each digit `d` from this list, we check `d>1` and `mod n d<1`, that is whether `d` divides `n`.
* If the checks are successful, we divide `n` by `d` and recursively apply `f`: `f$div n d`.
* Altogether, this yields a list of the minimal integers from all sub-trees of `n`.
* As the list might be empty, we append `n` to it and return the `minimum` of the list.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 8 bytes
```
÷DfḶß€Ṃo
```
[Try it online!](https://tio.run/##y0rNyan8///wdpe0hzu2HZ7/qGnNw51N@f@P7jncDmS7//9vqKNgrqNgaKCjYGQCxMYghrEJSMTCwABCAZWYGliamwF5RpamYFEgbWQBAA "Jelly – Try It Online")
### Alternate version, much faster, 9 bytes
```
÷DfÆḌß€Ṃo
```
[Try it online!](https://tio.run/##y0rNyan8///wdpe0w20Pd/Qcnv@oac3DnU35/4/uOdwOZLv//2@oo2Cuo2BooKNgZALExiCGsQlIxMLAAEIBlZgaWJqbAXlGlqZgUSBtZKGjYGFsbm4IYpiZmhpZmpiYmQAA "Jelly – Try It Online")
### How it works
```
÷DfḶß€Ṃo  Main link. Argument: n
 D        Decimal; yield the digits of n.
÷         Divide n by each of its digits.
   Ḷ      Unlength; yield [0, ..., n-1].
  f       Filter; keep quotients that belong to the range.
    ß€    Recursively map this link over the resulting list.
      Ṃ   Take the minimum. This yields 0 if the list is empty.
       o  Logical OR; replace 0 with n.
```
[Answer]
# [Python 2](https://docs.python.org/2/), 59 bytes
```
f=lambda a:min([f(a/k)for k in map(int,`a`)if k>1>a%k]+[a])
```
[Try it online!](https://tio.run/##RY/basMwDIav16fQTcFmgvoQx06hfZEQVq2taUjjhCa72NNnsilMF7/0w6fT/Ls@pmS2LZ6eNH7fCOg49km0UdBhkHF6wQB9gpFm0acVL3SRfYThrM@0H7rPljq5rfdl/brScl/gBK3QqCUKX1Qr1IqzqYo1VqGxpajQhwIEpd5oUNyalZ1Tja9R68KYxjFk/bs2AY3LJljvNTsbfMhTa@dMU1U1L@OQ3S7fn35GhGV65jf@Lz3uPuYXfwRRMCALsf0B "Python 2 – Try It Online")
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~52~~ 47 bytes
Competing for the non-exotic languages group! (Note: a good idea, if not golfing, is to add `.uniq` after `.digits` because all similar branches have similar results)
```
f=->n{n.digits.map{|x|x>1&&n%x<1?f[n/x]:n}.min}
```
[Try it online!](https://tio.run/##bc7LDoIwEAXQvV/RxMgO7PRBW6P4IYSFxmBY0BAfSQ3w7TVtgRRjl@d27szjff1YW5/SQvc6uzX35vXM2kvXD2YwBSSJ3pkjnOtS70110GPWNnq0HapLqNAWzS9NCwQbx@I/A47cM/ZO2K8HptF/x4ROztYu5FQv8TIRb5UYVuzARxwrka8imKqI4kuXC6hYnMjICQ@BpELAnPgBKWQ4N@ecKMZyf3RYAmC/ "Ruby – Try It Online")
## Explanation
```
f=->n{      # Function "f" n ->
   n.digits # n's digits (in reverse order (<- doesn't matter))
            # fun fact: all numbers always have at least one digit
    .map{|x|# Map function for every digit "x" ->
       x>1&&    # x is 2-9 and
       n%x<1    # n mod x == 0, or, "n is divisible by x"
       ? f[n/x] # then recursively find smallest of f[n/x]
       : n      # otherwise: n (no shortest path in tree)
     }.min  # Smallest option out of the above
            # if we reach a dead end, we should get n in this step
}
```
[Answer]
## [Common Lisp](https://common-lisp.net/), 136 bytes
```
(defun f(n)(apply 'min(or(loop for z in(map'list #'digit-char-p(write-to-string n))if(and(> z 1)(<(mod n z)1))collect(f(/ n z)))`(,n))))
```
[Try it online!](https://tio.run/##RU/LTsQwDLzzFaNFqI60FUlfSRHiWwh9LJHaJOpmBexhf724ZSVycDz2jO3pJneO60r9MF48RvKCbIzTD7LZeQoLTSFEjGHBFVyYbcxYkfCY9e7kUt592iWP9LW4NOQp5Oe0OH@CF8KNZH1PbyxUgl5pDj08rkIJ0YVpGrpEIz3vJSHe6cgSIdb/fWTxIXgnMiLFI0B6j0pCSf6LUqIo96SCNnvHSHnnGMmaLTKqZasbKLVzirZmUqnveWFQ1BswpdaKUWm02aY2dV20VdVULFR88wP21wcQXzfbhISD8/GSXnCzRwzfkS0N/R86h@mSXPAbuj0dwF5YB7tZ/AU "Common Lisp – Try It Online")
**Readable version:**
```
(defun f (n)
  (apply 'min
         (or (loop for z in (map 'list
                                 #'digit-char-p
                                 (write-to-string n))
                   if (and (> z 1)
                           (< (mod n z) 1))
                   collect (f (/ n z)))
             `(,n))))
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 21 bytes
```
Dðḍ>Ị{
DxÇ⁸:ß€µÇẸ$¡FṂ
```
[Try it online!](https://tio.run/##ATgAx/9qZWxsef//RMOw4biNPuG7insKRHjDh@KBuDrDn@KCrMK1w4fhurgkwqFG4bmC////ODM3NzEyOA "Jelly – Try It Online")
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 44 bytes
-7 bytes thanks to Misha Lavrov.
```
Min[#0/@(#/IntegerDigits@#⋂Range[#-1]),#]&
```
[Try it online!](https://tio.run/##HYm9CsIwFEZf5dKAKNySv9akg5LBxUFQ19IhSBozNEONk7i4@pa@SIwu53C@b7Lp6iabwsXmETb5EGJPGDVLQvcxOe/mXfAh3Qz5vF9nG73rSc2HFZJhkY9ziKknCBXUW6gQTvfgkhlNOYEaeHBUyBmKBoUskk0pzdifHFvWqTVy0bW/qUho1FIpLvQzfwE "Wolfram Language (Mathematica) – Try It Online")
[Answer]
## JavaScript (Firefox 30-57), 49 bytes
```
f=n=>Math.min(...(for(c of''+n)c<2|n%c?n:f(n/c)))
```
ES6-compatible version, 52 bytes:
```
f=n=>Math.min(...[...''+n].map(c=>c<2|n%c?n:f(n/c)))
```
```
<input type=number oninput=o.textContent=f(this.value)><pre id=o>
```
Originally I tried filtering out irrelevant digits but it turns out to be slightly longer at 54 bytes:
```
f=n=>Math.min(n,...(for(c of''+n)if(c>1&n%c<1)f(n/c)))
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), 33 bytes
```
{⍬≡d←o/⍨0=⍵|⍨o←1~⍨⍎¨⍕⍵:⍵⋄⌊/∇¨⍵÷d}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24TqR71rHnUuTAEy8/Uf9a4wsH3Uu7UGyMgHihjWARmPevsOAYmpQHErIH7U3fKop0v/UUc7SHTr4e0ptUCTFIyMDbjSFAyNLE2NLIAMM1NTI0sTEzMTIPvQCgVDBXMFQwMFIxMgaWFgACYNAQ "APL (Dyalog Unicode) – Try It Online")
**How?**
`⍎¨⍕⍵` - grab digits of `n`
`1~⍨` - excluding `1`s
`o/⍨` - filter by
`0=⍵|⍨o` - divisibility of `n` by the digit
`⍬≡...:⍵` - if empty, return `n`
`⌊/` - otherwise, return minimum of
`∇¨` - recursion for each number in
`⍵÷d` - the division of `n` by each of the digits filtered above
[Answer]
# [Kotlin](https://kotlinlang.org), ~~100~~ 99 bytes
```
fun f(i:Int):Int{return i.toString().map{it.toInt()-48}.filter{it>1&&i%it<1}.map{f(i/it)}.min()?:i}
```
## Beautified
```
fun f(i:Int):Int{
    return i.toString()
        .map { it.toInt()-48 }
        .filter { it >1 && i % it < 1}
        .map { f(i/it) }
        .min() ?: i
}
```
## Test
```
fun f(i:Int):Int{return i.toString().map{it.toInt()-48}.filter{it>1&&i%it<1}.map{f(i/it)}.min()?:i}
val tests = listOf(
        1 to 1,
        7 to 1,
        10 to 10,
        24 to 1,
        230 to 23,
        234 to 78,
        10800 to 1,
        10801 to 10801,
        50976 to 118,
        129500 to 37,
        129528 to 257,
        8377128 to 38783,
        655294464 to 1111)
fun main(args: Array<String>) {
    for ( test in tests) {
        val computed = f(test.first)
        val expected = test.second
        if (computed != expected) {
            throw AssertionError("$computed != $expected")
        }
    }
}
```
## Edits
* -1 [Mr. Xcoder](https://codegolf.stackexchange.com/users/59487)
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 15 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ÆDḊfD
:Çß€µÇ¡FṂ
```
**[Try it online!](https://tio.run/##y0rNyan8//9wm8vDHV1pLlxWh9sPz3/UtObQ1sPthxa6PdzZ9F/ncLsKUMT9/39DHXMdQwMdIxMdI2MgZWwC5FkYGIBJQx1TA0tzMx1DI0tTkBCQMrLQsTA2Nzc0sgAA "Jelly – Try It Online")**
I must admit that the `ß€` part is borrowed from [Erik's answer](https://codegolf.stackexchange.com/a/151850/59487). The rest is developed separately, partly because I don't even understand how the rest of that answer works anyway :P.
### How it works?
```
ÆDḊfD ~ Helper link (monadic). I'll call the argument N.
ÆD    ~ Take the divisors.
  Ḋ   ~ Dequeue (drop the first element). This serves the purpose of removing 1.
   fD ~ Take the intersection with the decimal digits.
:Çß€µÇ¡FṂ ~ Main link.
 Ç        ~ Apply the helper link to the first input.
:         ~ And perform element-wise integer division.
     Ç¡   ~ If the helper link applied again is non-empty*, then...
  ß€µ     ~ Apply this link to each (recurse).
       FṂ ~ Flatten and get the maximum.
```
\*I am pleasantly surprised that `¡` works like that on lists, because its normal meaning is *apply this **n** times*.
After Dennis explained why `ß€` doesn't need a conditional, we have this [12-byter](https://tio.run/##y0rNyan8//9wm8vDHV1pLlxWh9sPz3/UtObhzqYT6//rHG5XAXLc//831DHXMTTQMTLRMTIGUsYmQJ6FgQGYNNQxNbA0N9MxNLI0BQkBKSMLHQtjc3NDIwsA), or his 8 byte version :P.
[Answer]
# [R](https://www.r-project.org/), ~~101~~ 98 bytes
```
f=function(x,e=(d=x%/%10^(0:nchar(x))%%10)[d>1])"if"(sum(y<-which(!x%%e)),min(sapply(x/e[y],f)),x)
```
[Try it online!](https://tio.run/##DcjtCoIwFAbga0kYvAcWOiGoaN6IGMjcYYNc4gedXf3y@fmspbDlI7k9fhNEe4vJiqqVad5onsmFcYUQqTOonzozUBW5wnbMyK/rL0QXcBGlPJGeY8I2LssnQ2rf50HzuUKFYdrHrb1T@QM "R – Try It Online")
A ton of bytes go into extracting the digits and which ones divide `x`; perhaps another approach is necessary.
[Answer]
## Excel Vba, 153 bytes
First ever code-golf in the only language I know :(
Not exactly golf-friendly...
```
Function S(X)
S = X
For I = 1 To Len(CStr(X))
A = Mid(X, I, 1)
If A > 1 Then If X Mod A = 0 Then N = S(X / A)
If N < S And N > 0 Then S = N
Next I
End Function
```
Call like this:
```
Sub callS()
result = S(655294464)
MsgBox result
End Sub
```
I haven't a clue where to test this online.
[Answer]
# Perl 5, 87 + 1 (`-p`) = 88 bytes
```
$r=0,map{$\=$_,$r++if!$\|$_<$\;for$i(/[2-9]/g){$_%$i||$h{$_/$i}++}}$_,keys%h;$r&&redo}{
```
[try it online](https://tio.run/##DcvdCsIgGAbgawnejcINY@hgrF1JhgS59tGP8q2TUG8929lz8gTHT10KeDo2r2uIMBNsAxaC5h1Mgj3BjLNn0F6eu3a4yPshwlaglLBskqAsRM5be7jvWi0juK7Z3XyOpfRad4NSvfr58CH/Xksb/g)
[Answer]
# PHP, 120 bytes
```
<?php function f($n){$r=array_map(function($x)use($n){return$x>1&&!($n%$x)?f($n/$x):$n;},str_split($n));return min($r);}
```
[Try it online!](https://tio.run/##VZHdboMwDIXveQqvYh1IdCPhJ2Gs7YMwVCEEAmkFlASpU8WzMwdKYbmx/Z0cO1a6qhvHr3NXdVD2Ta7qtoHSMhv7bopjJkT2e7lmnbVolnmze1lMF0ShetGYtxPZ71@QvKJ21t4PTD7NJh4cqcRFdj@10gY7nh1wrbGPsONhNFUhlYQjJAbgSYgDJHXmnG1y4mLhLhX1NxL1UKPeWqLI@GrkrvuvEXf1EB0WFrgRC5GR1UWjQNs8tiWU46DgibjHGNHM44w/54dBQCPfD/UT8aRGGhtG2YoiyyuwHvtmEqbMhvvkKvKqnUmCS8IbwOFwAozv@i8e3HZg993sYjCG8Q8)
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 49 bytes
```
f(n)=vecmin([if(d<2||n%d,n,f(n/d))|d<-digits(n)])
```
[Try it online!](https://tio.run/##HY3bCsIwEER/JRSEBLaY@wWqP1J8KMaWgIZQiyDk3@PGlz07u8NMWfY0bqW1lWZ2@Tzur5TpnFYaJ1lrPkXIgK9zZKzGaYxpS8cbrTfWllKeX5rJeCVlT/nAdehiID2LASGzAAeCg9QgFUJpVJ7z/xRgeHAWhAymnxDSg1fOCaQ1RgatrcaiHw "Pari/GP – Try It Online")
]