-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathDigitalRoot.java
40 lines (36 loc) · 961 Bytes
/
DigitalRoot.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main.java.kyu6;
/**
* Digital root
*
* https://www.codewars.com/kata/541c8630095125aba6000c00
*
* Details:
*
* Digital root is the recursive sum of all the digits in a number.
*
* Given n, take the sum of the digits of n. If that value has more than one digit,
* continue reducing in this way until a single-digit number is produced.
* This is only applicable to the natural numbers.
*/
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class DigitalRoot {
public static int digital_root(int n) {
int i = n;
if (i / 10 == 0) {
return i;
} else {
int sum = 0;
while (i > 0) {
sum += i % 10;
i /= 10;
}
return digital_root(sum);
}
}
@Test
public void tests() {
assertEquals("Nope!", 7, digital_root(16));
assertEquals("Nope!", 6, digital_root(456));
}
}