[Question]
  [
This is different from [My Word can beat up your Word](https://codegolf.stackexchange.com/questions/1128/my-word-can-beat-up-your-word) as it is less complex and only requires you to calculate it, and not compare them.
To find the digital root, take all of the digits of a number, add them, and repeat until you get a one-digit number. For example, if the number was `12345`, you would add `1`, `2`, `3`, `4`, and `5`, getting `15`. You would then add `1` and `5`, giving you `6`.
### Your task
Given an integer ***N*** (0 <= ***N*** <= 10000) through **STDIN**, print the digital root of ***N***.
### Test cases
```
1 -> 1
45 -> 9
341 -> 8
6801 -> 6
59613 -> 6
495106 -> 7
```
Remember, this is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the code with the smallest number of bytes wins.
  
[Answer]
## Pyke, 1 byte
```
s
```
[Try it here!](http://pyke.catbus.co.uk/?code=s&input=495106)
Takes the digital root of the input
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~7 5 4~~ 3 [bytes](https://github.com/DennisMitchell/jelly/wiki/Code-page)
```
ḃ9Ṫ
```
**[TryItOnline!](http://jelly.tryitonline.net/#code=4biDOeG5qg&input=&args=NTk2MTM)** or [all test cases](http://jelly.tryitonline.net/#code=4biDOeG5qgrFvMOH4oKsRw&input=&args=WzEsMTgsNDUsMzQxLDY4MDEsNTk2MTMsNDk1MTA2XQ)
### How?
The [digital root](http://mathworld.wolfram.com/DigitalRoot.html) is known to obey the formula (n-1)%9+1.  
This is the same as the last digit in bijective base 9  
(and due to implementation that `0ḃ9=[]` and `[]Ṫ=0` this handles the edge-case of zero).
```
ḃ9Ṫ - Main link: n
ḃ9  - convert to bijective base 9 digits (a list)
  Ṫ - tail (get the last digit)
```
[Answer]
# JavaScript (ES6), ~~16~~ 10 bytes
```
n=>--n%9+1
```
Test cases
```
let f =
n=>--n%9+1
console.log(f(1)); // -> 1
console.log(f(45)); // -> 9
console.log(f(341)); // -> 8
console.log(f(6801)); // -> 6
console.log(f(59613)); // -> 6
console.log(f(495106)); // -> 7
```
[Answer]
# Python, ~~16~~ 20 bytes
+4 bytes to handle edge case of zero.
```
lambda n:n and~-n%9+1
```
**[repl.it](https://repl.it/EHnu/2)**
[Answer]
# [MATL](http://github.com/lmendo/MATL), 3 bytes
```
9X\
```
[Try it online!](http://matl.tryitonline.net/#code=OVhc&input=WzEgNDUgMzQxIDY4MDEgNTk2MTMgNDk1MTA2XQ)
A lot of (now deleted answers) tried using modulo 9 to get the result. This is a great shortcut, but unfortunately does not work for multiples of 9. MATL has a function for modulo on the interval `[1, n]`. Using this modulo, we have `1 % 3 == 1, 2 % 3 == 2, 3 % 3 == 3, 4 % 3 == 1`, etc. This answer simply takes the input modulo nine using this custom modulo. 
[Answer]
## Mathematica, ~~27~~ 11 bytes
```
Mod[#,9,1]&
```
Mathematica's `Mod` takes a third parameter as an offset of the resulting range of the modulo. This avoids decrementing the input and incrementing the output.
[Answer]
## Julia, 12 bytes
```
!n=mod1(n,9)
```
or 
```
n->mod1(n,9)
```
`mod1` is an alternative to `mod` which maps to the range `[1, n]` instead of `[0, n)`.
[Answer]
# PHP, 15 Bytes
```
<?=--$argn%9+1;
```
Previous version PHP, 55 Bytes
```
$n=$argn;while($n>9)$n=array_sum(Str_split($n));echo$n;
```
[Answer]
# R, 72 67 29 bytes
Edit: Thanks to @rturnbull for shaving off two bytes.
```
n=scan();`if`(n%%9|!n,n%%9,9)
```
[Answer]
# [Retina](https://github.com/m-ender/retina/wiki/The-Language), 7 bytes
```
{`.
*
.
```
[Try it online!](https://tio.run/##K0otycxLNPyvquGe8L86QY9Li0vv/39DLhNTLmMTQy4zCwNDLlNLM0NjLhNLU0MDMwA "Retina – Try It Online")
I see lots of mathematical solutions, but in Retina the straightforward approach seems to be the best one.
### Explanation
`{`` makes the whole program run in a loop until the string doesn't change anymore. The loop consists of two stages:
```
.
*
```
Convert each digit to unary.
```
.
```
Count the number of characters (=convert the unary number to decimal).
This works because converting each digit to unary with no separator between digits creates a single unary number which is equal to the sum of all digits.
[Answer]
# Haskell, ~~35~~ 34 bytes
```
until(<10)$sum.map(read.pure).show
```
[Try it on Ideone.](http://ideone.com/lm3Tma)
**Explanation:**
```
until(<10)$sum.map(read.pure).show
                              show  -- convert int to string
               map(         ).      -- turn each char (digit) into
                        pure        --    a string 
                   read.            --    and then a number
           sum.                     -- sum up the list of numbers
until(<10)$                         -- repeat until the result is < 10
```
[Answer]
# Perl, 15 bytes
Includes +2 for `-lp`
Give input on STDIN
```
root.pl <<< 123
```
`root.pl`
```
#!/usr/bin/perl -lp
$_&&=~-$_%9+1
```
This is the boring solution that has already been given in many languages, but at least this version supports `0` too
More interesting doing real repeated additions (though in another order) is in fact only 1 byte longer:
```
#!/usr/bin/perl -p
s%%$_+=chop%reg
```
[Answer]
# [Husk](https://github.com/barbuz/Husk), 4 bytes
### Recursive method (4 bytes) (Credit to @ovs)
```
ωoΣd
```
```
      # implicit parameter ⁰ (refers to the last parameter)
   d  # list of digits in base 10
  Σd  # sum of list
 oΣd  # compose the two functions
ωoΣd  # repeat until the function returns the same result as the previous one
```
[Try it online!](https://tio.run/##yygtzv6f@6ip8f/5zvxzi1P@//8fbahjYqpjbGKoY2ZhYKhjamlmaKxjYmlqaGAWCwA)
### Modulus method (4 bytes)
```
→%9←
```
```
      # implicit parameter ⁰ (refers to the last parameter)
   ←  # previous number (n - 1)
 %9←  # modulus by 9
→%9←  # next number (n + 1)
```
[Try it online!](https://tio.run/##yygtzv6f@6ip8f@jtkmqlo/aJvz//z/aUMfEVMfYxFDHzMLAUMfU0szQWMfE0tTQwCwWAA)
[Answer]
## Java 7, 63 bytes
```
int f(int n){int s=0;for(;n>0;n/=10)s+=n%10;return s>9?f(s):s;}
```
Recursive function which just gets digits with mod/div. Nothing fancy.
## Cheap port
of [Jonathan Allan's](https://codegolf.stackexchange.com/a/97730/14215) would be a measly 28 bytes:
```
int f(int n){return~-n%9+1;}
```
[Answer]
## [Labyrinth](https://github.com/m-ender/labyrinth), 8 bytes
```
?(_9%)!@
```
using the equation `(n-1)%9+1`:
* `?` reads the input as decimal and pushes it to the stack
* `(` decrements the top of the stack
* `_` pushes a zero onto the top of the stack
* `9` push the top of the stack popped times 10 the digit (in this case, 9)
* `%` pops y, pops x, pushes x%y
* `)` increments the top of the stack
* `!` pops the top of the stack and out puts it as a decimal string
* `@` terminates the program
[Answer]
## Hexagony, ~~19~~ 15 bytes
```
.?<9{(/>!@!/)%' 
```
More Readable:
```
  . ? < 
 9 { ( /
> ! @ ! / 
 ) % ' .
  . . . 
```
[Try it online!](https://tio.run/##y0itSEzPz6v8/19BQU/BXsFGgUvBUqFaQUNBn8tOQVHBAYj1gWKaCqoK6gp6XCBVQPj/v4mlqaGBGQA)
-3 bytes by taking a different approach, making the 0 edge case trivial.
  
-1 byte by fixing 0 edge case bug
Using the formula ((n-1) mod 9) + 1 like a lot of other solutions aswell.
[Answer]
# [Japt](https://github.com/ETHproductions/japt) [`-h`](https://codegolf.meta.stackexchange.com/a/14339/), ~~6~~ 4 [bytes](https://en.wikipedia.org/wiki/ISO/IEC_8859-1)
```
Æ=ìx
```
[Try it](https://petershaggynoble.github.io/Japt-Interpreter/?v=1.4.6&flags=LWg&code=xj3seA&input=NjgwMQ)
```
Æ=ìx     :Implicit input of integer U
Æ        :Map the range [0,U)
 =       :  Reassign to U
  ì      :  Convert to digit array
   x     :  Reduce by addition
         :Implicit output of last element
```
[Answer]
# [Brachylog](http://github.com/JCumin/Brachylog), 9 bytes
```
#0|@e+:0&
```
[Try it online!](http://brachylog.tryitonline.net/#code=IzB8QGUrOjAm&input=NDk1MTA2&args=Wg)
### Explanation
```
#0            Input = Output = a digit
  |           OR
   @e         Split the input into a list of digits
     +        Sum
      :0&     Call this predicate recursively
```
### Alternative approach, 11 bytes
[```
:I:{@e+}i#0
```](http://brachylog.tryitonline.net/#code=Okk6e0BlK31pIzA&input=NDk1MTA2&args=Wg)
This one uses the meta-predicate `i - Iterate` to call `I` times the predicate `{@e+}` on the input. This will try values of `I` from `0` to infinity until one makes it so that the output of `i` is a single digit which makes `#0` true.
[Answer]
# [05AB1E](http://github.com/Adriandmen/05AB1E), 6 bytes
```
[SODg#
```
[Try it online!](http://05ab1e.tryitonline.net/#code=W1NPRGcj&input=NDk1MTA2)
**Explanation**
```
[        # infinite loop
 S       # split into digits
  O      # sum digits
   Dg#   # if length == 1: break
```
[Answer]
## Ruby, 12 bytes
```
->n{~-n%9+1}
```
[Answer]
## JavaScript (ES6), ~~41~~ 38 bytes
*Saved 3 bytes, thanks to Bassdrop Cumberwubwubwub*
Takes and returns a string.
```
f=s=>s[1]?f(''+eval([...s].join`+`)):s
```
### Test cases
```
f=s=>s[1]?f(''+eval([...s].join`+`)):s
console.log(f("1")); // -> 1
console.log(f("45")); // -> 9
console.log(f("341")); // -> 8
console.log(f("6801")); // -> 6
console.log(f("59613")); // -> 6
console.log(f("495106")); // -> 7
```
[Answer]
# [CJam](https://sourceforge.net/p/cjam/), ~~19~~ 13 bytes
```
r{:~:+_s\9>}g
```
[Interpreter](http://cjam.aditsu.net/#code=r%7B%3A%7E%3A%2B_s%5C9%3E%7Dg&input=12345)
Explanation:
```
r{:~:+_s\9>}g Code
r             Get token
 {:~:+_s\9>}  Block: :~:+_s\9>
   ~          Eval
  :           Map
     +        Add
    :         Map
      _       Duplicate
       s      Convert to string
        \     Swap
         9    9
          >   Greater than
            g Do while (pop)
```
Thanks to 8478 (Martin Ender) for -6 bytes.
---
# CJam, 6 bytes
```
ri(9%)
```
Suggested by 8478 (Martin Ender). [Interpreter](//cjam.aditsu.net#code=ri(9%25)&input=12345)
I was thinking about it, but Martin just got it before me. Explanation:
```
ri(9%) Code
r      Get token
 i     Convert to integer
  (    Decrement
   9   9
    %  Modulo
     ) Increment
```
[Answer]
# [Factor](https://factorcode.org), 24
*Smart*, *mathy* answer.
```
[ neg bitnot 9 mod 1 + ]
```
**63** for dumb iterative solution:
```
[ [ dup 9 > ] [ number>string >array [ 48 - ] map sum ] while ]
```
[Answer]
# J, 8 bytes
```
**1+9|<:
```
Uses the formula `d(n) = ((n-1) mod 9) + 1` with the case `d(0) = 0`.
## Usage
```
   f =: **1+9|<:
   (,.f"0) 0 1 45 341 6801 59613 495106
     0 0
     1 1
    45 9
   341 8
  6801 6
 59613 6
495106 7
```
## Explanation
```
**1+9|<:  Input: integer n
      <:  Decrement n
    9|    Take it modulo 9
  1+      Add 1 to it
*         Sign(n) = 0 if n = 0, else 1
 *        Multiply and return
```
[Answer]
# Pyth - ~~7~~ ~~4~~ ~~6~~ 7 bytes
Not the best one, but still beats a decent amount of answers:
```
|ejQ9 9
```
Like the previous version, but handling also cases of multiples of 9, using logical or.
---
**This version fails the 45 testcase**:
```
ejQ9
```
**Explanation:**
```
 jQ9  -> converting the input to base 9
e     -> taking the last digit
```
[Try it here](https://pyth.herokuapp.com/?code=%26Qh%25tQ9&input=495106&debug=0)
[Try the previous version here!](https://pyth.herokuapp.com/?code=ejQ9&input=495106&debug=0)
---
**Previous solutions:**
```
&Qh%tQ9
```
**Explanation**:
```
    tQ    -> tail: Q-1
   %tQ9   -> Modulo: (Q-1)%9
  h%tQ9   -> head: (Q-1)%9+1
&Qh%tQ9   -> Logical 'and' - takes the first null value. If Q is 0 - returns zero, otherwise returns the (Q-1)%9+1 expression result
```
You're invited to [try it here](https://pyth.herokuapp.com/?code=%26Qh%25tQ9&input=495106&debug=0)!
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), ~~15~~ 9 bytes bytes
```
××1+9|-∘1
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R24TD0w9PN9S2rNF91DHDECikYMCVpmAIxCamQMLYBMQ0szAAUaaWZobGIBlLU0MDMwA "APL (Dyalog Unicode) – Try It Online")
[Answer]
## Python 2, 54 51 bytes
```
i=input()
while~-len(i):i=`sum(map(int,i))`
print i 
```
Thanks to Oliver and Karl Napf for helping me save 3 bytes
[Answer]
# Python, 45 bytes
```
f=lambda x:x[1:]and f(`sum(map(int,x))`)or x
```
Takes the argument as a string.
[Answer]
# C, ~~64~~ 29 bytes
C port from [Jonathan Allan](https://codegolf.stackexchange.com/a/97730/53667)'s answer (with special case 0).
```
f(i){return i>0?~-i%9+1:0;}
```
Previous 64 byte code:
```
q(i){return i>9?i%10+q(i/10):i;}
f(i){i=q(i);return i>9?f(i):i;}
```
`q` takes the cross sum and `f` repeats taking the cross sum until a single digit.
[Answer]
# [Retina](http://github.com/mbuettner/retina), 15 bytes
```
.+
$*
1{9}\B
1
```
[Try it online!](http://retina.tryitonline.net/#code=JShHYAouKwokKgoxezl9XEIKCjE&input=MQo0NQozNDEKNjgwMQ) (The first line enables a linefeed-separated test suite.)
### Explanation
```
.+
$*
```
Convert input to unary.
```
(1{9})*\B
```
Take 1-based modulo by removing nines that have at least one more character after them.
```
1
```
Count the remaining number of 1s to convert back to decimal.
]