Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions src/main/java/org/fundacionjala/coding/cynthia/DigitalRoot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package org.fundacionjala.coding.cynthia;

/**
* In this kata, you must create a digital root function.
* <p>
* 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 that value has two digits,
* continue reducing in this way until a single-digit number is produced.
* This is only applicable to the natural numbers.
* <p>
* Here's how it works (Ruby example given):
* <p>
* sumOfDigits(16)
* => 1 + 6
* => 7
* <p>
* sumOfDigits(942)
* => 9 + 4 + 2
* => 15 ...
* => 1 + 5
* => 6
* <p>
* sumOfDigits(132189)
* => 1 + 3 + 2 + 1 + 8 + 9
* => 24 ...
* => 2 + 4
* => 6
* <p>
* sumOfDigits(493193)
* => 4 + 9 + 3 + 1 + 9 + 3
* => 29 ...
* => 2 + 9
* => 11 ...
* => 1 + 1
* => 2
*/

public class DigitalRoot {

/**
* @param num with the digits.
* @return the sum of digits.
*/
public int sumOfDigits(int num) {
int res = 0;
while (num != 0) {
res += num % 10;
num = num / 10;
}
return res >= 10 ? sumOfDigits(res) : res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.fundacionjala.coding.cynthia;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

/**
* Created by Cynthia on 9/11/2017.
*/
public class DigitalRootTest {

/**
*
*/
@Test
public void tests() {
DigitalRoot digital = new DigitalRoot();
assertEquals(7, digital.sumOfDigits(16));
assertEquals(6, digital.sumOfDigits(942));
assertEquals(6, digital.sumOfDigits(132189));
assertEquals(2, digital.sumOfDigits(493193));
assertEquals(9, digital.sumOfDigits(54));
assertEquals(1, digital.sumOfDigits(100));
}
}