[Question]
  [
The crazy mathematician owns a wide collection of numbers, and therefore the space he has left is quite limited. To save some, he must fold his integers, but unfortunately he is really lazy. Your task, if you wish to help him, is to create a function / program that folds a given positive integer for our number maniac.
# How to fold an integer?
If it is evenly divisible by the sum of its digits, divide it by the sum of its digits. If it doesn't meet that requirement, take its remainder when divided by the sum of its digits. Repeat the process until the result reaches `1`. The folded integer is the number of operations you had to perform. Let's take an example (say `1782`):
1. Get the sum of its digits: `1 + 7 + 8 + 2 = 18`. `1782` is evenly divisible by `18`, so the next number is `1782 / 18 = 99`.
2. `99` is not evenly divisible by `9 + 9 = 18`, hence we take the remainder: `99 % 18 = 9`.
3. `9` is obviously divisible by `9`, so we divide it and obtain `1`.
The result is `3`, because 3 operations were required in order to reach `1`.
## Rules and Specs
* Some integers might have the sum of digits equal to `1`, such as `10` or `100`. Your program doesn't need to handle such cases. That means, you will be guaranteed that the integer given as input doesn't have the sum of digits equal to `1`, and no operation with the given integer will result in a number whose sum of digits is `1` (except for `1` itself, which is the "target"). For example, you will never receive `10` or `20` as input.
* The input will be a positive integer higher than `1`.
* **[Default Loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default)** apply.
* You can take input and provide output by **[any standard mean](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods)**.
---
# Test Cases
```
Input     -> Output
2         -> 1
5         -> 1
9         -> 1
18        -> 2
72        -> 2
152790    -> 2
152       -> 3
666       -> 3
777       -> 3
2010      -> 3
898786854 -> 4
```
Here is **[a program](https://tio.run/##lZHLjoMwDEX3@QorUlUi0DBoFkiV6C/MZnZtFzzcqSUwNAlFfD1jWuYltYuuovje@B473ehPLb9NE0MGxD4g7nofGKNKKbwq1dm5qj9OKDJ5ymvgvinQAjkdOW8DNqF@2bM2Sg0nqhEYtpBsFJQQZpAooKOUVuD6JmjyThJ8BLeHxkAmKeKFJee9Q5t7ahl0OHvKufl3TqTJAV6Q6xEqupCjQuKKEbzQSXtoj0DeifYph4S0MODVWQm9pOZcXb0WXV97mWCjI47ju2RGoBjiOLsPrgBrh0@Rc@ufpP/HutyanLhCO5M/WuoNffWIfPnT9Z7XC2m00@0P/pA7iTn3ZLGSAX4VJ9u0@Ec77MptcjDTlKbpFw)** that lets you visualize the process and try more test cases.
---
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code *in each language* (scored in bytes) wins!
  
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), ~~13~~ 12 bytes
```
[¼DSO‰0Kθ©#®
```
[Try it online!](https://tio.run/##MzBNTDJM/f8/@tAel2D/Rw0bDLzP7Ti0UvnQuv//LSwtzC3MLExNAA "05AB1E – Try It Online")
**Explanation**
```
[               # start loop
 ¼              # increment counter
  D             # duplicate current value
   SO           # sum the digits in the copy
     ‰          # divmod the current value by its digit-sum
      0K        # remove 0 from the resulting list
        θ       # pop the last element
         ©      # store a copy in register
          #     # if the current value is 1, break
           ®    # push the copy from register
                # implicitly output counter
```
[Answer]
# [Python 2](https://docs.python.org/2/), ~~63~~ 57 bytes
-1 thanks to totallyhuman  
-1 thanks to Mr. Xcoder  
-4 thanks to reffu
```
def f(n):a=sum(map(int,`n`));return n>1and-~f(n%a or n/a)
```
[Try it online!](https://tio.run/##HU7BCsIwFLvvK95FaOGpa3FtN5k/IsIKblhxb6PtDl789fpYLiEhJFm/@bWQLuU5TjAJkp3v0zaL2a8iUMaBBimvccxbJKCb8vQ8/jh38LBEoLOXJY8pJ@jhrhEahBZBOQTLSjXatvXOCMYYdq1F0LVi07XOOuOay6OqJu4KEAj2rq4Cxhp5H1KOIsjT572lLFQtkU8GWf4 "Python 2 – Try It Online")
[Answer]
# Haskell, ~~85~~ 78 bytes
```
f 1=0
f n|r<1=1+f(n`div`s)|1<2=1+f r where s=sum(read.pure<$>show n);r=n`rem`s
```
Saved 7 bytes thanks to Bruce Forte.
[Try it online.](https://tio.run/##VZDBboMwEETv/oqRlQMWaoTTtKUUR2oPlaJGyiE/YCOMQAET2VAu/DslNKncuezqzWp3tKVyZ13X01SAi4gUMKNNueBhERiZV9/SsZGnmyuAxVBqq@GE65vAapWvL73V6WrnynaAYW9WGGl1I900k77OPzSSBHvT4WH3V44IGFGQ9xGJDCOUEBkWCVz67tTZgwE9flGC/xrRdnOMoXLaH12Bfr7vDwkowhDBkkixa0/xewmZ9s2MEdKoysxL8nY5Mr/ATxXd4MaH/A4jHvn88cbj1/glfo6ftr65JdMP)
[Answer]
# JavaScript (ES6), ~~66~~ ~~58~~ ~~51~~ 49 bytes
Takes input as an integer. Returns `false` for `0` or `1` and throws an overflow error when it encounters any number whose digits add up to `1`.
```
f=n=>n>1&&f(n%(x=eval([...""+n].join`+`))||n/x)+1
```
* 8 bytes saved with help from [Justin](https://codegolf.stackexchange.com/users/69583/justin-mariner).
---
## Test it
```
o.innerText=(
f=n=>n>1&&f(n%(x=eval([...""+n].join`+`))||n/x)+1
)(i.value=898786854);oninput=_=>o.innerText=f(+i.value)
```
```
<input id=i type=number><pre id=o>
```
[Answer]
## [Husk](https://github.com/barbuz/Husk), 12 bytes
```
←€1¡Ṡ§|÷%oΣd
```
[Try it online!](https://tio.run/##yygtzv6vkKtR/KipsUjz0Lb/j9omPGpaY3ho4cOdCw4trzm8XTX/3OKU////G3GZcllyGVpwmRtxGZoamVsagCguMzMzLnNzcy4jA0MDLgtLC3MLMwtTEwA "Husk – Try It Online")
## Explanation
```
←€1¡Ṡ§|÷%oΣd  Implicit input, e.g. n=1782
    Ṡ§|÷%oΣd  This part defines the transformation.
         oΣ   Sum of
           d  digits: s=18
    Ṡ   %     n mod s: 0
     §|       or (take this branch if last result was 0)
       ÷      n divided by s: 99
   ¡          Iterate the transformation: [1782,99,9,1,1,1,...
 €1           Index of 1 (1-based): 4
←             Decrement: 3
              Print implicitly.
```
[Answer]
# [C# (.NET Core)](https://www.microsoft.com/net/core/platform), 87 bytes
```
n=>{int i=0,k,l;for(;n>1;++i){for(l=n,k=0;l>0;l/=10)k+=l%10;n=n%k>0?n%k:n/k;}return i;}
```
[Try it online!](https://tio.run/##fdExa4UwEAfw2XyKWx4o@voSqRqbxg6FTu1QOnQWm0pI3glJLBTxs1tt5/TgDo7fwX@4wZ@Hyalt9hpHePv2QV0FIYPtvYdXshDYy4c@6AG@Jv0BL73GNIM/OOppxuFeYyj27mAECRvKbtk30JIWprDic3KpwI6JPNfZcmxWYmEkFbbb@yIZzUwu7YlRgRJPpqMP@7zDixGrU2F2CFqsmyDJ44R@surm3emgnjWqdEzLLItIFZU2KoxHqYknsapsWvofR62u63hi00StpCweyFve8JpXt8fFSpLk919k3X4A "C# (.NET Core) – Try It Online")
Lambda function that takes and returns an integer.
[Answer]
# [Japt](https://github.com/ETHproductions/japt), ~~22~~ ~~19~~ 17 bytes
*-3 bytes thanks to @Shaggy.*  
*-2 bytes thanks to @ETHproductions*
```
ìx
>1©1+ßU%VªU/V
```
[Try it online!](http://ethproductions.github.io/japt/?v=1.4.5&code=Cux4Cj4xqTEr31UlVqpVL1Y=&input=ODk4Nzg2ODU0)
[Answer]
# [Retina](https://github.com/m-ender/retina), 100 bytes
```
$
;
{`(.+);
$1$*1;$&
(?<=;.*)\d(?=.*;)
$*
.*;1;(.*)
$.1
r`(1)*(\3)*;(1+);
$#1;$#2;1
0;(.*);|;.*;
$1;
```
[Try it online!](https://tio.run/##HYwxDsIwEAT7/UYOdD4ky3coNuiIUvKJFEaCgoYiooO/G5NqpdHOrI/383VrO77WRnB8KsdDcJCSqNMePF8mjxKWO89TFA8gQV917hQUFWtlDcLLMYizbvbQ3cFckbabf3viH/XWDCPO0BOKQUdDzhmlFFjS9AM "Retina – Try It Online") Link only includes smaller test cases as the larger ones take too long.
[Answer]
# Mathematica, 73 bytes
```
(t=#;For[r=0,t>1,r++,If[(s=Mod[t,g=Tr@IntegerDigits@t])<1,t=t/g,t=s]];r)&
```
[Answer]
# PHP, 68+1 bytes
unary output:
```
for($n=$argn;$n>1;$n=$n%($s=array_sum(str_split($n)))?:$n/$s)echo 1;
```
decimal output, 73+1 bytes:
```
for($n=$argn;$n>1;$i++)$n=$n%($s=array_sum(str_split($n)))?:$n/$s;echo$i;
```
Run as pipe with `-nR` or [try it online](http://sandbox.onlinephpfunctions.com/code/c8b2510a51d7ad689b4bf8ac7f6a4e081645a62b).
---
The Elvis operator requires PHP 5.3 or later. For older PHP, replace `?:` with `?$n%$s:` (+5 bytes).
[Answer]
# Ruby, 46 bytes
```
f=->n{s=n.digits.sum;n<2?0:1+f[n%s<1?n/s:n%s]}
```
[Answer]
# [Haskell](https://www.haskell.org/), ~~94~~ ~~93~~ ~~89~~ 88 bytes
This feels really long..
```
length.fst.span(/=1).iterate g
g x|(d,m)<-x`divMod`sum[read[d]|d<-show x]=last$m:[d|m<1]
```
[Try it online!](https://tio.run/##Fc27CsIwFIDh3ac4g0MLNlLwEiWZOgsdhVowmEuDSVqao3bosxvr8I8ffyfiUzmXNHC4JaeCwY7oiCQOImRbXubEohoFKjArA9OcyY3PWTHdpX1fenmPL9@MSshGtrNkRez6D0wtdyLi2p8bOXtWtskLG5bDMNqAQEAv/RFwxsAorPqAKmBM9ESP9ED3u@9DO2FiKq5VXf8A "Haskell – Try It Online")
Thanks @Laikoni & @nimi for golfing off 1 byte each!
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~83~~ ~~81~~ ~~76~~ 73 bytes
```
i,k,s;f(n){k=n;for(i=0;k>1;i++,n=k=k%s?:k/s)for(s=0;n;n/=10)s+=n%10;n=i;}
```
[Try it online!](https://tio.run/##dc7BisIwEAbge58iCIWEVkyytkk3xD178gW8LF0qITiKqSfx2buThd0aNw5zmX8@humXh76fJlf7OpiBArt5C2Y4Xaiz3PiNMK6qarDe@jJ8vPtVYHEZcAkGVlZwFioLpcDZOnOfjp8OKCtuBSHY54uDcaCLLZyvI4m13JDddcRpDwtmigcjyW@hEfux/EJSD1QydDNrXrAmZd0L1qVM6AcmZyZ06pTMO/X0nWik6njm3k/@z8433xKbwrZtsxDzpy@VykLMUyi54DkY81TqTivd6mYd5XqWf3nk92L6Bg "C (gcc) – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 12 bytes
```
dDS$Ṛȯ/µÐĿL’
```
[Try it online!](https://tio.run/##AScA2P9qZWxsef//ZERTJOG5msivL8K1w5DEv0zigJn///84OTg3ODY4NTQ "Jelly – Try It Online")
[Answer]
# Pyth, ~~20~~ 14 bytes
```
tl.ue-.DNsjNT0
```
[Try it here.](http://pyth.herokuapp.com/?code=tl.ue-.DNsjNT0&input=898786854&debug=0)
[Answer]
# Perl, ~~71~~ bytes, ~~64~~ bytes, 63 bytes
```
-pl
$c=0;while($_>1){$s=0;$s+=$_ for/./g;$_=$_%$s?$_%$s:$_/$s;++$c};$_=$c
```
[Try it online](https://tio.run/##HYnLCsIwEEX38xUuRlCCzQOTSQzVT8kiVC2EJjRCF@KvG2s393DOLcOcdGsYe@GX55iGA4arPL6xrgEr6zHs7nnmHX94DKvtsd62vWDgWD1jGD/bFVtToMGBtEAKpFbkxB9gjAEiAiWkAOssWWP1@ZvLa8xTbaeSfg)
EDIT: saved 7 bytes, thanks to Xcali's comment
```
-p
while($_>1){$s=0;$s+=$_ for/./g;$_=$_%$s?$_%$s:$_/$s;++$c}$_=$c
```
EDIT: since 5.14 non destructive substitution s///r
```
-pl
while($_>1){$s=eval s/\B/+/gr;$_=$_%$s?$_%$s:$_/$s;++$c}$_=$c
```
[Answer]
# Dyalog APL, 36 bytes
```
{x←+/⍎¨⍕⍵⋄1=⍵:0⋄0=x|⍵:1+∇⍵÷x⋄1+∇x|⍵}
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24TqCiChrf@ot@/Qike9Ux/1bn3U3WJoC6StDIAsA9uKGhDbUPtRRzuQcXh7BUgexANL1AJNUTDiSlMwBWJLIDa0ABLmIBFDUyNzSwMIA0iamZmBZMzNgaSRgSFIwsLSwtzCzMLUBAA "APL (Dyalog Unicode) – Try It Online")
**How?**
```
{
   x←+/⍎¨⍕⍵      ⍝ x = digit sum
   1=⍵:0         ⍝ if arg = 1: bye
   0=x|⍵:1+∇⍵÷x  ⍝ if arg divisible by x: recurse with arg/x
   1+∇x|⍵        ⍝ recurse with arg mod x
}
```
[Answer]
# [Gaia](https://github.com/splcurran/Gaia), 13 bytes
```
-@{:ΣZ¤∨)‡}°\
```
[Try it online!](https://tio.run/##S0/MTPz/X9eh2urc4qhDSx51rNB81LCw9tCGmP//LSwtzC3MLExNAA "Gaia – Try It Online")
### Explanation
```
-              Push -1 (this will be the counter)
 @             Push input (the starting number)
  {:ΣZ¤∨)‡}°   Repeat this block until the results of 2 consecutive runs are the same:
   :            Copy the number
    Σ           Digital sum
     Z          Divmod number by digital sum
      ¤         Swap
       ∨        Logical or: left-most non-zero out of (number mod sum, number div sum)
        )‡      Increment the counter
            \  Delete the final 1, implicitly print the counter
```
[Answer]
## Matlab, 150 bytes
```
function[d]=X(x) 
d=0;while ~strcmp(x,'1')z='sum(str2num(x(:)))';a=eval(['rem(',x,',',z,')']);y=num2str(a*(a>0)+eval([x,'/',z])*(a==0));x=y;d=d+1;end
```
Inputs should be given to the function as a string, such as X('152'). 
The function works by while looping and incrementing d. The `x=y;` line was necessary to avoid an error of Matlab trying to read and overwrite a variable value at the same time, apparently, which was a new one on me.
Ungolfed: 
```
function[d]=X(x) 
d=0;
while ~strcmp(x,'1')
    z='sum(str2num(x(:)))';
    a=eval(['rem(',x,',',z,')']);
    y=num2str(a*(a>0)+eval([x,'/',z])*(a==0));
    x=y;
    d=d+1;
end
```
[Answer]
# [Haskell](https://www.haskell.org/), 68 bytes
```
f 1=0
f n=1+[f x|x<-[mod n,div n]<*>[sum$read.pure<$>show n],x>0]!!0
```
[Try it online!](https://tio.run/##VY9Pa4NAEMXv@yleFg@xJmFt09aIKzSHQGgghx4lkJVdUapr8E/jwe9urU3Kdi4z83vDvJlU1J8qz4chgcsZSaC560QJur4LllFRSuiFzL6gT8FDGNVtYVVKyNWlrVRghXVaXkdp0YXsNJuxYezbXG4VfB973WAZ/qUj5jYRON9HzojRQ3AeYwqOS9t8NNVBgx7fKcH/6FE2qaquWa3MUQt097Y/@KBwHMyne4T9U1P8OiFWphjbhBQi0@MSWU4m4@PmVewGH03o3iFzmcmfbtzbeK/ei/e8NsU1Gb4B "Haskell – Try It Online") Based on [w0lf's answer](https://codegolf.stackexchange.com/a/139297/56433).
[Answer]
# [R](https://www.r-project.org/), 85 bytes
```
function(n){while(n>1){n="if"(x<-n%%(d=sum(n%/%10^(nchar(n):0)%%10)),x,n/d)
F=F+1}
F}
```
Anonymous function that returns the required output.
[Verify all test cases!](https://tio.run/##FcrRCoIwFIDhex9DGJxDJ9xGbjNalz5GINpQWCexJEN89mVXP3z8Uwo@hZnb9/BkYFw//RDvwFeFK/t8CDkslyMLAZ1/zQ9gUQglb8Bt30z7f5YodkCkhbjoMKt9fVBbVm8pNuMYv9CCppIqUo6sJlVqW8l/yBhD1lrSUklylbPOuPKEFDD9AA "R – Try It Online")
]