Digital root的求解

源于hdu1013

题目描述:

The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.

For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3 and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.

维基百科对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.

于是老老实实按照题目描述以及定义写了代码:

 1 #include <stdio.h>
 2
 3 #define MAX 8192
 4
 5 char buf[MAX];
 6
 7 int
 8 main(int argc, char **argv)
 9 {
10     int i, sum, input;
11
12     while (fgets(buf, MAX, stdin)) {
13         if (buf[0] == ‘0‘ && buf[1] == ‘\n‘) {
14             break;
15         }
16         sum = 0;
17         input = 0;
18
19         for (i = 0; i < MAX; i++) {
20             if (buf[i] == ‘\n‘) {
21                 break;
22             }
23             input += buf[i] - ‘0‘;
24         }
25
26         while ( (input != 0) || (sum / 10 != 0) ) {
27             if (input == 0) {
28                 input = sum;
29                 sum = 0;
30             }
31             sum += input % 10;
32             input /= 10;
33         }
34
35         printf("%d\n", sum);
36     }
37
38     return 0;
39 }

正确通过。

百度了一下,发现一个关于digital root的结论,一个数字模9同余与各位数位上的和,即n % 9的结果等同于各个数位的和%9。

证明如下:

  设自然数N=a[n]a[n-1]…a[0],其中a[0],a[1]、…、a[n]分别是个位、十位、…上的数字

  再设M=a[0]+a[1]+…+a[n]

  求证:N≡M(mod 9).

 证明:
     ∵ N=a[n]a[n-1]…a[0]=a[n]*10^n+a[n-1]*10^(n-1)+…+a[1]*10+a[0].
    又∵ 1≡1(mod 9),
        10≡1(mod 9),
        10^2≡1(mod 9),
          … 
        10^n≡1(mod 9).
    上面这些同余式两边分别同乘以a[0]、a[1]、a[2]、…、a[n],再相加得:
      a[0]+a[1]*10+…+a[n]*10^n≡(a[0]+a[1]+…+a[n])(mod 9),
                    即 N≡M(mod 9),得证。

这样就能够求解数字n的digital root(下面写作dr)。

分三种情况:

第一,若n=0,则dr=0;

第二,若n != 0, n % 9 = 0,则dr=9;

第三,若n % 9 != 0,则dr = n % 9。

将这三种情况合并,则有公式如下:

知道了这个结论,我们就很容易编程解决digital root的求解问题了。



参考:

http://en.wikipedia.org/wiki/Digital_root 维基百科

http://www.cnblogs.com/Rinyo/archive/2012/12/20/2826755.html Rinyo的博客。

时间: 2024-11-09 02:01:21

Digital root的求解的相关文章

数字根(digital root)

来源: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 = 38 , the process is like:  3 + 8 = 11 ,  1 + 1 = 2 . Since  2  has only one digit, r

Digital root(数根)

关于digital root可以参考维基百科,这里给出基本定义和性质. 一.定义 数字根(Digital Root)就是把一个数的各位数字相加,再将所得数的各位数字相加,直到所得数为一位数字为止.而这个一位数便是原来数字的数字根.适用范围为正整数和零.例如:65536,6+5+5+3+6=25,2+5=7,故数根为7. 二.性质 1. 任何数加减9的数字根还是它本身. 2. 9乘任何数字的数字根都是9. 3. 数字根的三则运算 (1). 两数之和的数字根等于这两个数的数字根的和数字根 (2).

SGU[118] Digital Root

Description 描述 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 of f(n). For example, digital root of 987 is 6. Your task is to fi

Codeforces 10C Digital Root 规律题

题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<string> #include<stdlib.h> #include<algorithm> using nam

[codewars_python]Sum of Digits / Digital Root

Instructions 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 more than one digit, continue reducing in this way unti

DIgital Root 的推导

背景 在LeetCode上遇到这道题:Add Digits 题目很简单,但是如果要用 O(1) 时间复杂度,不要涉及循环或递归来解答的话,我就不知道如何下手了. 于是我找了一下别人的解法,发现涉及到一个 Digital Root 的原理(由于维基百科打不开,所以我觉得有必要记录一下我搜集到的信息和理解). Digital Root 我是从这个网站上看到它的推导过程,但是为了防止以后这些引用的网站不存在或者访问不了,还是得自立更生写一下. 首先,A ≡ B mod C, ≡ 这个符号, 表示 A

Digital Root - SGU 118(高精度运算)

题目大意:有K组测试数据,然后每组有N个正整数,A1,A2,A3.....An,求出 A1 + A1*A2 + A1*A2*A3 + .......A1*A2*...An 的数根. 分析:有个对9取余的定理是可以直接求树根的,不过拿来玩大数运算也不错.ps.每位可以保存9位数,保存10位数会溢出. 高精度代码如下: ===========================================================================================

CodeForces 10C. Digital Root

求A,B,C ∈[1,N] && A*B != C,d(A*B) == d(C)的组数. 首先要知道d(x) = (x%9 == 0 ? 9 : x%9); 那么则会有A*B == C,则必有d(A*B) == d(C). 若不考虑A*B != C,则答案既是ans[x]*ans[y]*ans[d(x*y)],ans[x]为d(i) == x的个数,显然x∈[1,9]. 当考虑A*B != C时,则需要从ans[x]*ans[y]*ans[d(x*y)] - sum. sum = sigm

Codeforces 10C Digital Root 法冠军

主题链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<set> #include<vector> #include<map> #include<math.h> #include<string> #include<stdlib.h> #include<algorithm> using nam