[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 until a single-digit number is produced. This is only applicable to the natural numbers.

Here‘s how it works:

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

My solution:

def digital_root(n):
    lst = [int(x) for x in str(n)]
    result = sum(lst)
    if result < 10:
        return result
    else:
        return digital_root(result)

Best solution:

def digital_root(n):
    return n if n < 10 else digital_root(sum(map(int,str(n))))

原文地址:https://www.cnblogs.com/icris/p/10659236.html

时间: 2024-10-13 23:41:03

[codewars_python]Sum of Digits / Digital Root的相关文章

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的求解

源于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 s

DIgital Root 的推导

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

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

Codeforces Round #277.5 (Div. 2)C. Given Length and Sum of Digits...(贪心)

传送门 Description You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the d

Sum of Digits is Prime

Sum of Digits is Prime Daoyi Peng August 19, 2014 For an integer $q\geqslant2$ let $s_q(n)$ denote the $q$-ary sum-of-digits function of a non-negative integer $n$, that is, if $n$ is given by its $q$-ary digits expansion $n=\sum\limits_{k=0}^{r} a_k

Codeforces Round #277.5 (Div. 2)——C贪心—— Given Length and Sum of Digits

You have a positive integer m and a non-negative integer s. Your task is to find the smallest and the largest of the numbers that have length m and sum of digits s. The required numbers should be non-negative integers written in the decimal base with

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).

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