mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-26 05:59:22 +08:00
Format code with prettier (#3375)
This commit is contained in:
@ -39,8 +39,7 @@ package com.thealgorithms.maths;
|
||||
class DigitalRoot {
|
||||
|
||||
public static int digitalRoot(int n) {
|
||||
if (single(n) <= 9) // If n is already single digit than simply call single method and return the value
|
||||
{
|
||||
if (single(n) <= 9) { // If n is already single digit than simply call single method and return the value
|
||||
return single(n);
|
||||
} else {
|
||||
return digitalRoot(single(n));
|
||||
@ -49,17 +48,15 @@ class DigitalRoot {
|
||||
|
||||
// This function is used for finding the sum of digits of number
|
||||
public static int single(int n) {
|
||||
if (n <= 9) // if n becomes less than 10 than return n
|
||||
{
|
||||
if (n <= 9) { // if n becomes less than 10 than return n
|
||||
return n;
|
||||
} else {
|
||||
return (n % 10) + single(n / 10); // n % 10 for extracting digits one by one
|
||||
return (n % 10) + single(n / 10); // n % 10 for extracting digits one by one
|
||||
}
|
||||
} // n / 10 is the number obtainded after removing the digit one by one
|
||||
} // n / 10 is the number obtainded after removing the digit one by one
|
||||
// Sum of digits is stored in the Stack memory and then finally returned
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Time Complexity : O((Number of Digits)^2) Auxiliary Space Complexity :
|
||||
* O(Number of Digits) Constraints : 1 <= n <= 10^7
|
||||
|
Reference in New Issue
Block a user