问题阐述会是这样的:

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 111 + 1 = 2. Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

这其实是一个digital root的问题。

digital root的定义如下:

  The digital root (also repeated digital sum) of a non-negative integer is the (single digit) value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.

For example, the digital root of 65,536 is 7, because 6 + 5 + 5 + 3 + 6 = 25 and 2 + 5 = 7.

编程题中一般会要求在O(1)时间算出一个数的digital root,这时候就不能用上述思想解答问题了。

通用公式:

 或者是

所有的原理其实和模运算以及同余定理有关:

考虑12345 =  1 × 10,000 + 2 × 1,000 + 3 × 100 + 4 × 10 + 5.

同时10 i= 9 + 1; 100 i= 99 + 1,所以又可以写成:

12,345 = 1 × (9,999 + 1) + 2 × (999 + 1) + 3 × (99 + 1) + 4 × (9 + 1) + 5.

展开后:

12,345 = (1 × 9,999 + 2 × 999 + 3 × 99 + 4 × 9) + (1 + 2 + 3 + 4 + 5).

这样便满足了数根的思想,计算数根的一次迭代,当然(1 + 2 + 3 + 4 + 5)=15 又可以接着迭代,总之是:

数根是模9的余数是因为  因此 这样便有 ,也就有如下推论:

这里要强调为什么当数字是9的倍数时,dr(n)是9?

例如:18 = 10 + 9

$18 \equiv 0  \pmod{9}$

但 $10 + 9 \equiv 1 + 8 \pmod{9}$,莫着急,这只是表象, $1 + 8 = 9 \equiv 0 \pmod{9}$

所以9的倍数的数根也可以用(mod 9)运算,只不过由于数根只在1-9之间,所以为零时只要换成9即可,毕竟$9 \equiv 0 \pmod{9}$

至于

     也是这个道理,数根只能在1-9之间,而(mod 9)的数域在0-8之间, 所以先对数字减1然后再补1即可折中等效了。

关键点是理解为什么由

$a = b + c$
$b \equiv r_1 \pmod{9} $
$c \equiv r_2 \pmod{9} $

可推导出:

$a \equiv r_1 + r_2\pmod{9}$

提示:把数写成 $n = mq + r $,依据一条推论:

推论   a≡b(mod m)的充要条件是a=mt+b(t为整数)。

表示对模m同余关系的式子叫做模m的同余式,简称同余。

参考资料:

digital root

同余定理

A NEAT NUMBER TRICK: DIGITAL ROOTS AND MODULO-9 ARITHMETIC

leetcode--add digits

digital root问题的更多相关文章

  1. Digital root(数根)

    关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这 ...

  2. 数字根(digital root)

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

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

  4. Sum of Digits / Digital Root

    Sum of Digits / Digital Root In this kata, you must create a digital root function. A digital root i ...

  5. 1. 数字根(Digital Root)

    数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1 ...

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

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

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

  8. 数学 - SGU 118. Digital Root

    Digital Root Problem's Link Mean: 定义f(n)为n各位数字之和,如果n是各位数,则n个数根是f(n),否则为f(n)的数根. 现在给出n个Ai,求出A1*A2*…*A ...

  9. 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root

    题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...

随机推荐

  1. linux中BASH_SOURCE[0]

    在C/C++中,__FUNCTION__常量记录当前函数的名称.有时候,在日志输出的时候包含这些信息是非常有用的.而在Bash中,同样有这样一个常量FUNCNAME,但是有一点区别是,它是一个数组而非 ...

  2. Eclipse代码风格设置

    在编写代码的过程中,代码的呈现形式是通过eclipse的Formatter配置文件所控制的.我们可以按照自己的习惯生成属于自己的代码风格配置文件,方便规范以后的代码编写形式.具体的操作步骤如下所示:( ...

  3. win7+IE11 中开发工具报错occurredJSLugin.3005解决办法

    系统环境 win7+IE11 报错描述: Exception in window.onload: Error: An error has ocurredJSPlugin.3005 Stack Trac ...

  4. jQuery 获取 多个 复选框 和 javascript 对比

    $('input[name="teams"]:checked').size() // 全选 $("#quanteam").bind("click&qu ...

  5. windows中path环境变量即时生效

    修改PATH后,打来CMD命令行,输入 “set PATH=C” (不会真的改变PATH变量值,但会重新读取一次PATH值),关掉CMD窗口再打开.OK 不放心可以 echo %PATH% 检视一下.

  6. Python中metaclass解释

    Classes as objects 首先,在认识metaclass之前,你需要认识下python中的class.python中class的奇怪特性借鉴了smalltalk语言.大多数语言中,clas ...

  7. JIRA官方:JIRA源代码集成

    防火墙后的Git 使用Atlassian Stash创建和管理Git存储库,设置细粒度的权限并在代码上协作.这一切—安全.快速.可靠,更重要的是,可以部署在防火墙后面.JIRA问题关键字自动将JIRA ...

  8. poj1581

    A Contesting Decision Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 2519   Accepted: ...

  9. iOS Xcode工程目录的 folder 和 group的区别(蓝色和黄色文件夹的区别)

    1. 来自 http://blog.csdn.net/fanjunxi1990/article/details/9352917 XCode工程目录里面,有时你会发现2个不同颜色的文件夹,一种是蓝色的, ...

  10. OC基础6:多态、动态类型和动态绑定

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.关于SEL类型的数据: (1).SEL ...