[FEATURE]: Add Digital Root Algorithm to JavaScript/Maths and its recursive implementation · Issue #1747 · TheAlgorithms/JavaScript · GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

[FEATURE]: Add Digital Root Algorithm to JavaScript/Maths and its recursive implementation #1747

Open
@HRIDYANSHU054

Description

@HRIDYANSHU054

Motivation

Digital root algorithm is a very simple but powerful tool used in various fields such as checksum calculations (e.g., ISBN validation), cyclic redundancy checks, and understanding number properties in modular arithmetic. Plus, it offers a great way for beginners to learn and understand recursion problems (if its recursive implementation is also included)

Adding this feature helps people learn both the mathematical approach (constant time complexity) and recursive thinking. It would be useful for anyone interested in algorithms and number theory.

Examples

  1. For n = 15:

    • Calculation:
      digital_root(15) = 1 + (15 - 1) % 9
                       = 1 + 14 % 9
                       = 1 + 5
                       = 6
      
    • Output: 6
    • Explanation: The digital root of 15 is 6 because the sum of its digits 1 + 5 = 6.
  2. For n = 123:

    • Calculation:
      digital_root(123) = 1 + (123 - 1) % 9
                        = 1 + 122 % 9
                        = 1 + 5
                        = 6
      
    • Output: 6
    • Explanation: The digital root of 123 is 6 because the sum of its digits 1 + 2 + 3 = 6.

Possible workarounds

No response

Additional information

Algorithm overview

The digital root of a number is the value obtained by repeatedly summing the digits of the number until a single digit is reached.
This issue proposes two implementations

  1. Best-case constant time complexity algorithm
    This calculates the digital root with constant time complexity using modulo arithmetic.
export function digitalRoot(num) {
  if (num < 0) num = -num;
  return num === 0 ? num : 1 + ((num - 1) % 9);
}
  1. Recursive Implementation
    This has been proposed as a learning tool rather than for practical application. This recursive solution can help beginners in understanding recursion, and help them delve further into algorithms.
export function digitalRoot(num) {
  if (num < 0) num = -num;
  if (num < 10) return num;
  const sum = (num % 10) + digitalRoot(Math.floor(num / 10));
  return sum >= 9 ? sum - 9 : sum;
}

Also, if kindly assign this to me if this is accepted for implementation

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      TMZ Celebrity News – Breaking Stories, Videos & Gossip

      Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

      Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

      🎥 Watch TMZ Live

      TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beat—watch now and see what’s trending in Hollywood.