From 7c75bebb08d19e7058970a5d80363d682fc47f11 Mon Sep 17 00:00:00 2001 From: Ankana Pari <143877643+ankana2113@users.noreply.github.com> Date: Sat, 26 Oct 2024 10:10:25 +0530 Subject: [PATCH 1/5] added digital root algorithm --- Maths/DigitalRoot.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Maths/DigitalRoot.js diff --git a/Maths/DigitalRoot.js b/Maths/DigitalRoot.js new file mode 100644 index 0000000000..efbeb28cf8 --- /dev/null +++ b/Maths/DigitalRoot.js @@ -0,0 +1,34 @@ +export const digitalRoot = (num: number): number => { + /** + * Calculates the digital root of a number in constant time. + * @param {number} num - The number to compute the digital root for. + * @returns {number} The digital root of the given number. + * + * @example + * digitalRoot(456) // returns 6 + * digitalRoot(-999) // returns 9 + * digitalRoot(0) // returns 0 + */ + if (num < 0) num = -num; + return num === 0 ? num : 1 + ((num - 1) % 9); +}; +/*------------------------------------------------------------------------------------*/ + +export const digitalRootRecursive = (num: number): number => { + /** + * Calculates the digital root of a number using recursion. + * @param {number} num - The number to compute the digital root for. + * @returns {number} The digital root of the given number. + * + * @example + * digitalRoot(456) // returns 6 + * digitalRoot(999) // returns 9 + * digitalRoot(0) // returns 0 + */ + if (num < 0) num = -num; // Handle negative input by converting to positive + if (num < 10) return num; // Base case for single-digit number + + // Recursive case: sum digits and continue to reduce + const sum = (num % 10) + digitalRootRecursive(Math.floor(num / 10)); + return digitalRootRecursive(sum); // Call digitalRoot recursively to reduce to single digit +}; From 6d1849ab2072949bd39086804965610e14ac4337 Mon Sep 17 00:00:00 2001 From: Ankana Pari <143877643+ankana2113@users.noreply.github.com> Date: Sat, 26 Oct 2024 10:18:02 +0530 Subject: [PATCH 2/5] minor fixes --- Maths/DigitalRoot.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Maths/DigitalRoot.js b/Maths/DigitalRoot.js index efbeb28cf8..99f7082fa4 100644 --- a/Maths/DigitalRoot.js +++ b/Maths/DigitalRoot.js @@ -1,4 +1,4 @@ -export const digitalRoot = (num: number): number => { +export const digitalRoot = (num) => { /** * Calculates the digital root of a number in constant time. * @param {number} num - The number to compute the digital root for. @@ -14,7 +14,7 @@ export const digitalRoot = (num: number): number => { }; /*------------------------------------------------------------------------------------*/ -export const digitalRootRecursive = (num: number): number => { +export const digitalRootRecursive = (num) => { /** * Calculates the digital root of a number using recursion. * @param {number} num - The number to compute the digital root for. From a807c3657ddec1afd602cbd8481615d391483590 Mon Sep 17 00:00:00 2001 From: Ankana Pari <143877643+ankana2113@users.noreply.github.com> Date: Sat, 26 Oct 2024 10:24:27 +0530 Subject: [PATCH 3/5] feat: added digital root algorithm and its recursive implementation --- Maths/DigitalRoot.js | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/Maths/DigitalRoot.js b/Maths/DigitalRoot.js index 99f7082fa4..43d1c13908 100644 --- a/Maths/DigitalRoot.js +++ b/Maths/DigitalRoot.js @@ -1,14 +1,14 @@ export const digitalRoot = (num) => { /** - * Calculates the digital root of a number in constant time. - * @param {number} num - The number to compute the digital root for. - * @returns {number} The digital root of the given number. - * - * @example - * digitalRoot(456) // returns 6 - * digitalRoot(-999) // returns 9 - * digitalRoot(0) // returns 0 - */ + * Calculates the digital root of a number in constant time. + * @param {number} num - The number to compute the digital root for. + * @returns {number} The digital root of the given number. + * + * @example + * digitalRoot(456) // returns 6 + * digitalRoot(-999) // returns 9 + * digitalRoot(0) // returns 0 + */ if (num < 0) num = -num; return num === 0 ? num : 1 + ((num - 1) % 9); }; @@ -16,15 +16,15 @@ export const digitalRoot = (num) => { export const digitalRootRecursive = (num) => { /** - * Calculates the digital root of a number using recursion. - * @param {number} num - The number to compute the digital root for. - * @returns {number} The digital root of the given number. - * - * @example - * digitalRoot(456) // returns 6 - * digitalRoot(999) // returns 9 - * digitalRoot(0) // returns 0 - */ + * Calculates the digital root of a number using recursion. + * @param {number} num - The number to compute the digital root for. + * @returns {number} The digital root of the given number. + * + * @example + * digitalRoot(456) // returns 6 + * digitalRoot(999) // returns 9 + * digitalRoot(0) // returns 0 + */ if (num < 0) num = -num; // Handle negative input by converting to positive if (num < 10) return num; // Base case for single-digit number From e6cda60c542fcf75ccf2bba42c424c8ef48c3dd4 Mon Sep 17 00:00:00 2001 From: Ankana Pari <143877643+ankana2113@users.noreply.github.com> Date: Sat, 26 Oct 2024 10:39:33 +0530 Subject: [PATCH 4/5] prettier fixes --- Maths/DigitalRoot.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/Maths/DigitalRoot.js b/Maths/DigitalRoot.js index 43d1c13908..0eb3b7dbe0 100644 --- a/Maths/DigitalRoot.js +++ b/Maths/DigitalRoot.js @@ -1,29 +1,27 @@ -export const digitalRoot = (num) => { +const digitalRoot = (num) => { /** * Calculates the digital root of a number in constant time. * @param {number} num - The number to compute the digital root for. * @returns {number} The digital root of the given number. * - * @example - * digitalRoot(456) // returns 6 - * digitalRoot(-999) // returns 9 - * digitalRoot(0) // returns 0 + * @example digitalRoot(456) // returns 6 + * @example digitalRoot(-999) // returns 9 + * @example digitalRoot(0) // returns 0 */ if (num < 0) num = -num; return num === 0 ? num : 1 + ((num - 1) % 9); }; /*------------------------------------------------------------------------------------*/ -export const digitalRootRecursive = (num) => { +const digitalRootRecursive = (num) => { /** * Calculates the digital root of a number using recursion. * @param {number} num - The number to compute the digital root for. * @returns {number} The digital root of the given number. * - * @example - * digitalRoot(456) // returns 6 - * digitalRoot(999) // returns 9 - * digitalRoot(0) // returns 0 + * @example digitalRootRecursive(456) // returns 6 + * @example digitalRootrecursive(999) // returns 9 + * @example digitalRootRecursive(0) // returns 0 */ if (num < 0) num = -num; // Handle negative input by converting to positive if (num < 10) return num; // Base case for single-digit number @@ -32,3 +30,8 @@ export const digitalRootRecursive = (num) => { const sum = (num % 10) + digitalRootRecursive(Math.floor(num / 10)); return digitalRootRecursive(sum); // Call digitalRoot recursively to reduce to single digit }; + +export { + digitalRoot, + digitalRootRecursive +} From 968cf2908b774bb808968436857acd3d3d671a2d Mon Sep 17 00:00:00 2001 From: Ankana Pari <143877643+ankana2113@users.noreply.github.com> Date: Sat, 26 Oct 2024 10:52:14 +0530 Subject: [PATCH 5/5] minor checks --- Maths/DigitalRoot.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Maths/DigitalRoot.js b/Maths/DigitalRoot.js index 0eb3b7dbe0..2e6703e76d 100644 --- a/Maths/DigitalRoot.js +++ b/Maths/DigitalRoot.js @@ -31,7 +31,4 @@ const digitalRootRecursive = (num) => { return digitalRootRecursive(sum); // Call digitalRoot recursively to reduce to single digit }; -export { - digitalRoot, - digitalRootRecursive -} +export { digitalRoot, digitalRootRecursive };