Title: [FEATURE]: Add Digital Root Algorithm to JavaScript/Maths and its recursive implementation · Issue #1747 · TheAlgorithms/JavaScript · GitHub
Open Graph Title: [FEATURE]: Add Digital Root Algorithm to JavaScript/Maths and its recursive implementation · Issue #1747 · TheAlgorithms/JavaScript
X Title: [FEATURE]: Add Digital Root Algorithm to JavaScript/Maths and its recursive implementation · Issue #1747 · TheAlgorithms/JavaScript
Description: 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,...
Open Graph Description: 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...
X Description: 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...
Opengraph URL: https://github.com/TheAlgorithms/JavaScript/issues/1747
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"[FEATURE]: Add Digital Root Algorithm to JavaScript/Maths and its recursive implementation","articleBody":"### Motivation\n\nDigital 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)\r\n\r\nAdding 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.\n\n### Examples\n\n1. **For `n = 15`**:\r\n - Calculation:\r\n ```\r\n digital_root(15) = 1 + (15 - 1) % 9\r\n = 1 + 14 % 9\r\n = 1 + 5\r\n = 6\r\n ```\r\n - Output: `6`\r\n - Explanation: The digital root of `15` is `6` because the sum of its digits `1 + 5 = 6`.\r\n\r\n2. **For `n = 123`**:\r\n - Calculation:\r\n ```\r\n digital_root(123) = 1 + (123 - 1) % 9\r\n = 1 + 122 % 9\r\n = 1 + 5\r\n = 6\r\n ```\r\n - Output: `6`\r\n - Explanation: The digital root of `123` is `6` because the sum of its digits `1 + 2 + 3 = 6`.\n\n### Possible workarounds\n\n_No response_\n\n### Additional information\n\nAlgorithm overview\r\n\r\nThe digital root of a number is the value obtained by repeatedly summing the digits of the number until a single digit is reached.\r\nThis issue proposes two implementations \r\n\r\n1. Best-case constant time complexity algorithm\r\nThis calculates the digital root with constant time complexity using modulo arithmetic.\r\n```javascript\r\nexport function digitalRoot(num) {\r\n if (num \u003c 0) num = -num;\r\n return num === 0 ? num : 1 + ((num - 1) % 9);\r\n}\r\n```\r\n\r\n2. Recursive Implementation\r\nThis 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.\r\n```javascript\r\nexport function digitalRoot(num) {\r\n if (num \u003c 0) num = -num;\r\n if (num \u003c 10) return num;\r\n const sum = (num % 10) + digitalRoot(Math.floor(num / 10));\r\n return sum >= 9 ? sum - 9 : sum;\r\n}\r\n```\r\n\r\nAlso, if kindly assign this to me if this is accepted for implementation","author":{"url":"https://github.com/HRIDYANSHU054","@type":"Person","name":"HRIDYANSHU054"},"datePublished":"2024-10-22T19:25:59Z","url":"https://github.com/TheAlgorithms/JavaScript/issues/1747"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e9161c56-c47f-a75a-aaa0-025b4a469c02 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE98:2C4BED:3009787:3D06CEE:6B01C6FB |
| html-safe-nonce | 8f31369e74769faa397f547f0d177d0c76583491acad02c812f924ee49aab00b |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTk4OjJDNEJFRDozMDA5Nzg3OjNEMDZDRUU6NkIwMUM2RkIiLCJ2aXNpdG9yX2lkIjoiODQzNTAzNjQ1MjIzNjE1MDUyMyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | f1a816f0cc7a676f0711baceeb733946b57f31b64d1de610b837ce4531b2851c |
| hovercard-subject-tag | issue:2606291621 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/TheAlgorithms/JavaScript/1747/issue_layout |
| twitter:image | https://opengraph.githubassets.com/debffce30a300d47d1eb2ce742371c56fc3587f2e8c82b04ddfa31bfeca51bee/TheAlgorithms/JavaScript/issues/1747 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/debffce30a300d47d1eb2ce742371c56fc3587f2e8c82b04ddfa31bfeca51bee/TheAlgorithms/JavaScript/issues/1747 |
| og:image:alt | 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... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | HRIDYANSHU054 |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/TheAlgorithms/JavaScript git https://github.com/TheAlgorithms/JavaScript.git |
| octolytics-dimension-user_id | 20487725 |
| octolytics-dimension-user_login | TheAlgorithms |
| octolytics-dimension-repository_id | 97086543 |
| octolytics-dimension-repository_nwo | TheAlgorithms/JavaScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 97086543 |
| octolytics-dimension-repository_network_root_nwo | TheAlgorithms/JavaScript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width