-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathDigitalRootAlgorithm.js
More file actions
32 lines (28 loc) · 1.01 KB
/
DigitalRootAlgorithm.js
File metadata and controls
32 lines (28 loc) · 1.01 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
/**
* Digital Root of n by Recursive Approach
* Time Complexity : O(1) - Mathematical Formula
* Time Complexity : O(log n) - Recursive Approach
* For more info: https://en.wikipedia.org/wiki/Digital_root
*
* Mathematical Formula: DigitalRoot(n)= 1 + (n - 1) mod 9
* Recursive Formula: DigitalRoot(n)= n if n<10 else if digitalRoot(sumOfDigits)
*
* @param {number} n
* @returns {number}- digital root of n
* @description
* The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
*/
const digitalRoot = (n) => {
if (n === 0) return 0;
return 1 + (n - 1) % 9;
};
const recursiveDigitalRoot = (n) => {
if (n < 10) return n;
let sum = 0;
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return recursiveDigitalRoot(sum);
}
export { digitalRoot,recursiveDigitalRoot };