Sum of Digits / Digital Root
2015-07-09 15:30
435 查看
SumofDigits/DigitalRoot
Inthiskata,youmustcreateadigitalrootfunction.
Adigitalrootistherecursivesumofallthedigitsinanumber.Givenn,takethesumofthedigitsofn.Ifthatvaluehastwodigits,continuereducinginthiswayuntilasingle-digitnumberisproduced.Thisisonlyapplicabletothenaturalnumbers.
Here'showitworks(Rubyexamplegiven):
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
usingSystem; usingSystem.Linq; publicclassNumber { publicintDigitalRoot(longn) { //Yourawesomecodehere! while(n>9) { n=n.ToString().Select(c=>Convert.ToInt32(c.ToString())).Sum(); } return(int)n; } }
publicclassNumber
{
publicintDigitalRoot(longn)
{
return(int)((n-1)%9+1);
}
}
相关文章推荐
- 40、DrawerLayout使用详情
- Android Sdk 国内镜像下载地址
- android:textSize="20dp" 中20dp是什么意思?
- centos安装redis
- 一些编写高性能HTML应用的建议
- GRE写作必备句型
- jquery param()方法
- Python类私有方法的陷阱
- iOS判断网络连接类型
- 上篇几个函数
- 线程池和连接池的原理
- extjs插件开发上传下载文件简单案例
- Cookie——项目实战:运用Cookie技术,显示用户上次登录的时间
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- java加密技术
- Unity游戏开发使用Assetbundle加载场景的原理
- .Net 三款工作流引擎比较:WWF、netBPM 和 ccflow
- Activity切换中生命周期的交互
- 【转】[容器-docker]Ubuntu 系列安装 Docker
- 8位位图转换成24位位图