Digital Root 的推导】的更多相关文章

背景 在LeetCode上遇到这道题:Add Digits 大意是给一个数,把它各位数字相加得到一个数,如果这个数小于10就返回,不然继续 addDigits(这个相加得到的数). 题目很简单,但是如果要用 O(1) 时间复杂度,不要涉及循环或递归来解答的话,我就不知道如何下手了. 于是我找了一下别人的解法,发现涉及到一个 Digital Root 的原理(由于维基百科打不开,所以我觉得有必要记录一下我搜集到的信息和理解). Digital Root 我是从这个网站上看到它的推导过程,但是为了防…
Not long ago Billy came across such a problem, where there were given three natural numbers A, B and C from the range [1, N], and it was asked to check whether the equation AB = C is correct. Recently Billy studied the concept of a digital root of a…
关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.适用范围为正整数和零.例如:65536,6+5+5+3+6=25,2+5=7,故数根为7. 二.性质 1. 任何数加减9的数字根还是它本身. 2. 9乘任何数字的数字根都是9. 3. 数字根的三则运算 (1). 两数之和的数字根等于这两个数的数字根的和数字根      …
来源:LeetCode 258  Add Dights Question:Given a non-negative integer  num , repeatedly add all its digits until the result has only one digit. For example: Given  num =  , the process is like:   + =  ,   + =  . Since    has only one digit, return it. Fo…
digital root = n==0 ? 0 : n%9==0 ? 9:n%9;可以简单证明一下n = a0*n^0 + a1*n^1 + ... + ak * n^kn%9 = a0+a1+..+ak然后,数学归纳易知结论是正确的.因此9个状态就够了,表示%9的结果.这里需要特殊处理0, 表示状态为0. /* 4351 */ #include <iostream> #include <sstream> #include <string> #include <m…
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 w…
问题阐述会是这样的: 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 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. Follow up:Could you do it…
数字根(Digital Root)就是把一个自然数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.例如: 198的数字根为9(1+9+8=18,1+8=9). 性质说明 任何数加9的数字根还是它本身 小学学加法的时候我们都明白,一个数字加9,就是把十位加1,个位减1.因此十位加个位的和是不变的:如果有进位,即十位上是9,那么进位之后十位会变成0,百位会加1,道理和一个一位数加9是一样的. 9乘任何数字的数字根都是9 同样是小学时学乘法时,我们…
118. Digital Root time limit per test: 0.25 sec. memory limit per test: 4096 KB Let f(n) be a sum of digits for positive integer n. If f(n) is one-digit number then it is a digital root for n and otherwise digital root of n is equal to digital root o…
C. Digital Root 题目连接: http://www.codeforces.com/contest/10/problem/C Description Not long ago Billy came across such a problem, where there were given three natural numbers A, B and C from the range [1, N], and it was asked to check whether the equat…