Haven't any experience writing recursive functions.(Generally tend to avoid them - not sure if that's a negative). What do you think about the following?
This is also my first time using JUnit, I'd appreciate input if I'm doing something wrong there.
Challenge: Create a digital Root Function.
Specifications:
A digital root is the recursive sum of all the digits in a number.
Given n, take the sum of the digits of n.
If the resulting value has two digits, continue reducing until a single-digit number is produced.
This is only applicable to the natural numbers.
Solution:
public static int getDigitalRoot(int num) {
if (Integer.toString(num).length() == 1) { return num; }
int result = 0;
for (char c : Integer.toString(num).toCharArray()) {
result += Character.getNumericValue(c);
}
return getDigitalRoot(result);
}
Tests:
@Test
public void Tests() {
assertEquals(6 , Utilities.getDigitalRoot(942));
assertEquals(7, Utilities.getDigitalRoot(16));
}
Haven't any experience writing recursive functions.(Generally tend to avoid them - not sure if that's a negative). What do you think about the following?
This is also my first time using JUnit, I'd appreciate input if I'm doing something wrong there.
Challenge: Create a digital Root Function.
Specifications:
A digital root is the recursive sum of all the digits in a number.
Given n, take the sum of the digits of n.
If the resulting value has two digits, continue reducing until a single-digit number is produced.
This is only applicable to the natural numbers.
Solution:
public static int getDigitalRoot(int num) {
if (Integer.toString(num).length() == 1) { return num; }
int result = 0;
for (char c : Integer.toString(num).toCharArray()) {
result += Character.getNumericValue(c);
}
return getDigitalRoot(result);
}
Tests:
@Test
public void Tests() {
assertEquals(6 , Utilities.getDigitalRoot(942));
assertEquals(7, Utilities.getDigitalRoot(16));
}
Haven't any experience writing recursive functions.(Generally tend to avoid them - not sure if that's a negative). What do you think about the following?
This is also my first time using JUnit, I'd appreciate input if I'm doing something wrong there.
Challenge: Create a digital Root Function.
Specifications:
A digital root is the recursive sum of all the digits in a number.
Given n, take the sum of the digits of n.
If the resulting value has two digits, continue reducing until a single-digit number is produced.
This is only applicable to the natural numbers.
Solution:
public static int getDigitalRoot(int num) {
if (Integer.toString(num).length() == 1) { return num; }
int result = 0;
for (char c : Integer.toString(num).toCharArray()) {
result += Character.getNumericValue(c);
}
return getDigitalRoot(result);
}
Tests:
@Test
public void Tests() {
assertEquals(6 , Utilities.getDigitalRoot(942));
assertEquals(7, Utilities.getDigitalRoot(16));
}
Digital root recursive function
Haven't any experience writing recursive functions.(Generally tend to avoid them - not sure if that's a negative). What do you think about the following?
This is also my first time using JUnit, I'd appreciate input if I'm doing something wrong there.
Challenge: Create a digital Root Function.
Specifications:
A digital root is the recursive sum of all the digits in a number.
Given n, take the sum of the digits of n.
If the resulting value has two digits, continue reducing until a single-digit number is produced.
This is only applicable to the natural numbers.
Solution:
public static int getDigitalRoot(int num) {
if (Integer.toString(num).length() == 1) { return num; }
int result = 0;
for (char c : Integer.toString(num).toCharArray()) {
result += Character.getNumericValue(c);
}
return getDigitalRoot(result);
}
Tests:
@Test
public void Tests() {
assertEquals(6 , Utilities.getDigitalRoot(942));
assertEquals(7, Utilities.getDigitalRoot(16));
}