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). 两数之积的数字根等于这两个数的数字根的积的和数字根

  (3). 一个数字的n次幂的数字根等于这个数字的数字根的n次幂的和数字根

三、求数根公式

  digital root = (digital - 1) mod 9 + 1

四、Java实现

public class Solution {
    public int addDigits(int num) {
        int result = (num - 1)%9 + 1;
        return result;

    }
}
时间: 2025-01-14 12:21:33

Digital root(数根)的相关文章

如何证明一个数的数根(digital root)就是它对9的余数?

数根就是不断地求这个数的各位数之和,直到求到个位数为止.所以数根一定和该数模9同余,但是数根又是大于零小于10的,所以数根模9的余数就是它本身,也就是说该数模9之后余数就是数根. 证明: 假设有一个n位的10进制数,我们写成,其中表示从低到高的每一位因为 那么 也就是一个数和它的各数位之和的模9相同.不如我们把这个操作记为f即也就是所以也就是说每做一次这样的操作,它对于9的模始终是不变的所以最终求出的数根和原数对9的模相同. 例子:(12345) % 9 = (1 + 2 + 3 + 4 + 5

数字根(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

HDU-1013-Digital Roots(Java && 大数 && 数根)

Digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 57047    Accepted Submission(s): 17799 Problem Description The digital root of a positive integer is found by summing the digits of

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

DIgital Root 的推导

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

Openjudge-NOI题库-数根

题目描述 Description 数根可以通过把一个数的各个位上的数字加起来得到.如果得到的数是一位数,那么这个数就是数根.如果结果是两位数或者包括更多位的数字,那么再把这些数字加起来.如此进行下去,直到得到是一位数为止. 比如,对于24来说,把2和4相加得到6,由于6是一位数,因此6是24的数根.再比如39,把3和9加起来得到12,由于12不是一位数,因此还得把1和2加起来,最后得到3,这是一个一位数,因此3是39的数根. 输入输出格式 Input/output 输入格式: 一个正整数(小于1

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

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

zju 1115 Digital roots 数字根

#include <iostream> #include <string> using namespace std; int main() { string n; while(cin>>n,n!="0") { int s=0,l=n.length(); for(int i=0;i<l;i++) s+=n[i]-'0'; while(s>9) s=s/10+s%10; cout<<s<<endl; } return

[LeetCode]64. Add Digits数根

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 without any