Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Maths/DigitalRootAlgorithm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 };
Loading