Digit root of a positive number is the sum of all of its digits.
Finding this Digit root was needed in many problems which were appear in programming contests.
Here is the simplest algorithm(as I think up to now :-) ) to find the Digit root of any positive integer implemented using JavaScript.
function findDigRoot(n) { var root = 0; while (n > 0) { root += n ; n = Math.floor(n / 10); } return root; }
Feel free to comment below the post if you have a better, optimized solution or problems regarding the solution.
No comments:
Post a Comment