codewars053: 數根

Instructions:

Sum of Digits / Digital Rootjava

Solution:

//--https://www.codewars.com/kata/541c8630095125aba6000c00/train/java
//It might breach time limit if you don't make use of the formula.
// digital_root(n) = (n - 1) % 9 + 1
public class DRoot{
    public static int digital_root(int n){
        return (n - 1) % 9 + 1;
    }    
}

Example Tests:

To be done
https://www.codewars.com/kata/541c8630095125aba6000c00/train/java

Reference

the formula for Digital Rootgit

相關文章
相關標籤/搜索