forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericRoot.java
More file actions
48 lines (43 loc) · 1.23 KB
/
GenericRoot.java
File metadata and controls
48 lines (43 loc) · 1.23 KB
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
41
42
43
44
45
46
47
48
package com.thealgorithms.maths;
/**
* Calculates the generic root (repeated digital sum) of a non-negative integer.
* <p>
* For example, the generic root of 12345 is calculated as:
* 1 + 2 + 3 + 4 + 5 = 15,
* then 1 + 5 = 6, so the generic root is 6.
* <p>
* Reference:
* https://technotip.com/6774/c-program-to-find-generic-root-of-a-number/
*/
public final class GenericRoot {
private static final int BASE = 10;
private GenericRoot() {
}
/**
* Computes the sum of the digits of a non-negative integer in base 10.
*
* @param n non-negative integer
* @return sum of digits of {@code n}
*/
private static int sumOfDigits(final int n) {
assert n >= 0;
if (n < BASE) {
return n;
}
return (n % BASE) + sumOfDigits(n / BASE);
}
/**
* Computes the generic root (repeated digital sum) of an integer.
* For negative inputs, the absolute value is used.
*
* @param n integer input
* @return generic root of {@code n}
*/
public static int genericRoot(final int n) {
int number = Math.abs(n);
if (number < BASE) {
return number;
}
return genericRoot(sumOfDigits(number));
}
}