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的更多相关文章

  1. [codewars_python]Sum of Digits / Digital Root

    Instructions In this kata, you must create a digital root function. A digital root is the recursive ...

  2. digital root问题

    问题阐述会是这样的: Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  3. 快速切题 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 ...

  4. Codeforces Beta Round #10 C. Digital Root 数学

    C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy ...

  5. Digital Root 的推导

    背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如 ...

  6. codeforces 10C Digital Root(非原创)

    Not long ago Billy came across such a problem, where there were given three natural numbers A, B and ...

  7. 数字根(digital root)

    来源:LeetCode 258  Add Dights Question:Given a non-negative integer  num , repeatedly add all its digi ...

  8. 【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+..+ ...

  9. 树根 Digital root

    数根 (又称数字根Digital root)是自然数的一种性质.换句话说.每一个自然数都有一个数根.数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相 ...

随机推荐

  1. 启发式搜索 A*算法的OC 实现

    前两天重新学习了下A*算法,上次学习A*算法已经是5年前了,看到网上铺天盖地的A*算法都是C.C++等等其他语言的,就是没有OC 的,所以抽空写了一份.今天太晚了就不说明A*算法的细节了,大家如果想学 ...

  2. 【mysql】【分组】后取每组的top2

    DROP TABLE IF EXISTS `tb1`; CREATE TABLE `tb1` ( `id` ) NOT NULL AUTO_INCREMENT, `a` ) DEFAULT NULL, ...

  3. OC2_数组操作

    // // main.m // OC2_数组操作 // // Created by zhangxueming on 15/6/11. // Copyright (c) 2015年 zhangxuemi ...

  4. STL容器与配接器

    STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector                             行为类似于数组,但可以根据要求 ...

  5. Percona-Server-5.5.15源码安装

    [root@localhost rpm]# ll total 19148 -rw-r--r-- 1 root root   562628 Jan 18  2007 bison-2.3-2.1.x86_ ...

  6. Windows Server R2服务器 IIS7 部署MVC3网站

    报错:调用 GetProcAddress 失败,在 ISAPI 筛选器 "C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi ...

  7. C#使用oledb方式将excel数据导入到datagridview后数据被截断为 255 个字符

    问题描述:在使用oledb方式将excel数据导入到datagridview中,在datagridview单元格中的数据没有显示全,似乎只截取了数据源中的一段 解决方案:1.关于该问题,微软官方答案: ...

  8. 重学C语言 -- printf,scanf

     printf();    用来显示格式串的内容          注意: 参数不可以换行,否则会出一个警告.       格式串中占位符比表达式数量多     会显示一个无意义值 格式串中占位符比表 ...

  9. 八,WPF 命令

    WPF命令模型 ICommand接口 WPF命令模型的核心是System.Windows.Input.ICommand接口,该接口定义了命令的工作原理,它包含了两个方法和一个事件: public in ...

  10. 用shell查找某个目录下最大文件

    网上资料学习: 1.查找当前目录下最大文件(包括子目录里文件): find . -type f -exec stat -c "%s %n" {} \; | sort -nr | h ...