Skip to content

Cynthia digital root#157

Open
CynthiaTerrazas wants to merge 4 commits intodevelopfrom
cynthia_Digital_Root
Open

Cynthia digital root#157
CynthiaTerrazas wants to merge 4 commits intodevelopfrom
cynthia_Digital_Root

Conversation

@CynthiaTerrazas
Copy link

In this kata, you must create a digital root function.

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.

Here's how it works (Ruby example given):

digital_root(16)
=> 1 + 6
=> 7

digital_root(942)
=> 9 + 4 + 2
=> 15 ...
=> 1 + 5
=> 6

digital_root(132189)
=> 1 + 3 + 2 + 1 + 8 + 9
=> 24 ...
=> 2 + 4
=> 6

digital_root(493193)
=> 4 + 9 + 3 + 1 + 9 + 3
=> 29 ...
=> 2 + 9
=> 11 ...
=> 1 + 1
=> 2

@codecov-io
Copy link

codecov-io commented Sep 11, 2017

Codecov Report

Merging #157 into develop will increase coverage by 0.59%.
The diff coverage is 100%.

Impacted file tree graph

@@              Coverage Diff              @@
##             develop     #157      +/-   ##
=============================================
+ Coverage      88.39%   88.98%   +0.59%     
- Complexity       492      524      +32     
=============================================
  Files            109      117       +8     
  Lines           1043     1099      +56     
  Branches         178      195      +17     
=============================================
+ Hits             922      978      +56     
  Misses           106      106              
  Partials          15       15
Impacted Files Coverage Δ Complexity Δ
.../org/fundacionjala/coding/cynthia/DigitalRoot.java 100% <100%> (ø) 4 <4> (?)
...n/java/org/fundacionjala/coding/yury/Frequent.java 100% <0%> (ø) 6% <0%> (?)
...org/fundacionjala/coding/richard/MostFrequent.java 100% <0%> (ø) 7% <0%> (?)
...rg/fundacionjala/coding/cynthia/SumOddNumbers.java 100% <0%> (ø) 2% <0%> (?)
...org/fundacionjala/coding/ovidio/CountingSheep.java 100% <0%> (ø) 2% <0%> (?)
.../java/org/fundacionjala/coding/ovidio/Persist.java 100% <0%> (ø) 4% <0%> (?)
...n/java/org/fundacionjala/coding/ovidio/Vowels.java 100% <0%> (ø) 4% <0%> (?)
...in/java/org/fundacionjala/coding/ovidio/DRoot.java 100% <0%> (ø) 3% <0%> (?)
...a/org/fundacionjala/coding/yury/EanValidation.java 88.88% <0%> (+3.17%) 5% <0%> (ø) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 537f014...6f124ff. Read the comment docs.

while (num != 0) {
mod = num % 10;
num = num / 10;
res = res + mod;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be

res += mod;

assertEquals("Nope!", digital.sumOfDigits(132189), 6);
assertEquals("Nope!", digital.sumOfDigits(493193), 2);
assertEquals("Nope!", digital.sumOfDigits(54), 9);
assertEquals("Nope!", digital.sumOfDigits(100), 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update this
expectedResult is first than actualResult. :)

num = num / 10;
res += mod;
}
return res >= 10 ? sumOfDigits(res) : res;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to avoid some unnecessary variables.
Try this.

    public int sumOfDigits(int num) {
        int res = 0;
        while (num != 0) {
            res += num % 10;
            num = num / 10;
        }
        return res >= 10 ? sumOfDigits(res) : res;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants