Sum of Digits / Digital Root
Sum of Digits / Digital Root
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
using System;
using System.Linq; public class Number
{
public int DigitalRoot(long n)
{
// Your awesome code here!
while (n > )
{
n = n.ToString().Select(c => Convert.ToInt32(c.ToString())).Sum();
}
return (int)n;
}
}
public class Number
{
public int DigitalRoot(long n)
{
return (int)((n - ) % + );
}
}
Sum of Digits / Digital Root的更多相关文章
- [codewars_python]Sum of Digits / Digital Root
Instructions In this kata, you must create a digital root function. A digital root is the recursive ...
- digital root问题
问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- 快速切题 sgu118. Digital Root 秦九韶公式
118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of ...
- Codeforces Beta Round #10 C. Digital Root 数学
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...
- Digital Root 的推导
背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...
- codeforces 10C Digital Root(非原创)
Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...
- 数字根(digital root)
来源:LeetCode 258 Add Dights Question:Given a non-negative integer num , repeatedly add all its digi ...
- 【HDOJ】4351 Digital root
digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ ...
- 树根 Digital root
数根 (又称数字根Digital root)是自然数的一种性质.换句话说.每一个自然数都有一个数根.数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相 ...
随机推荐
- 在Xcode6中找回失去的模板
[Add]2014.07.27 添加OC category.protocol模板 Xcode 6从beta 3开始在创建新项目窗口中移除了“Empty Application”,如下: 其他选项很不幸 ...
- asp:时间的显示
DateTime dt = DateTime.Now;// Label1.Text = dt.ToString();//2005-11-5 13:21:25// Label2.Text = ...
- 页面加载后的input change事件 1或2个框 ajax
数据层没有,js和bll直接链接,数据层用的hqew. js: window.onload = function () { //型号input 改变 事件 $("#typeofproduct ...
- GNU iconv
GNU iconv 一.关键函数 1.iconv_open() iconv_open(DestinationCharsets, SourceCharSets) 2.iconv() [XSI] [Opt ...
- c++ primer (5)2
第三章 1.头文件不应包含using声明,因为头文件的内容会拷贝到所有引用它的文件中去. 2.初始化string对象的方式: string s1; //默认初始化,s1是一个空串 string s2( ...
- C++ map.insert 传参类型不同,构造/析构次数不同
1. 传参方式 使用 insert 为 map 插值时,insert 的传参包含以下几种可能: make_pair 生成对象 pair(key_type, value_type) 生成对象 pair( ...
- BootstrapDialog.show函数底层简化
平台用的全部都是BootStrapDialog的弹窗,然后美工设计了一个统一的样式,每次写的时候,都要对其进行样式重写:写吐了快,所以对BootStrap.底层做了修改: 也就是说,只要你要写的界面包 ...
- web页面显示折叠树菜单笔记
zTree -- jQuery 树插件 http://pan.baidu.com/s/1skwh94h
- 《Effective Java》读书笔记
一.引言 1.几条基本规则:(清晰性和简洁性最为重要) 模块的用户永远也不应该被模块的行为所迷惑(那样就不清晰了),模块要尽可能小,但又不能太小 代码应该被重用,而不是被拷贝 模块之间的依赖性应该尽可 ...
- VC++对话框中添加状态栏的方法
方法一:1.添加成员变量CStatusBarCtrl m_StatusBar;2.在OnInitDialog()中加入: m_StatusBar.Create(WS_ ...