Sum the digits of a number

后端 未结 18 1964
抹茶落季
抹茶落季 2020-11-22 10:52

If I want to find the sum of the digits of a number, i.e.:

  • Input: 932
  • Output: 14, which is (9 + 3 + 2)
相关标签:
18条回答
  • 2020-11-22 11:22
    def sumOfDigits():
    
        n=int(input("enter digit:")) 
        sum=0
        while n!=0 :
    
            m=n%10
            n=n/10
            sum=int(sum+m)
    
        print(sum)
    
    sumOfDigits()
    
    0 讨论(0)
  • 2020-11-22 11:23

    A base 10 number can be expressed as a series of the form

    a × 10^p + b × 10^p-1 .. z × 10^0

    so the sum of a number's digits is the sum of the coefficients of the terms.

    Based on this information, the sum of the digits can be computed like this:

    import math
    
    def add_digits(n):
        # Assume n >= 0, else we should take abs(n)
        if 0 <= n < 10:
            return n
        r = 0
        ndigits = int(math.log10(n))
        for p in range(ndigits, -1, -1):
            d, n = divmod(n, 10 ** p)
            r += d
        return r
    
    

    This is effectively the reverse of the continuous division by 10 in the accepted answer. Given the extra computation in this function compared to the accepted answer, it's not surprising to find that this approach performs poorly in comparison: it's about 3.5 times slower, and about twice as slow as

    sum(int(x) for x in str(n))
    
    0 讨论(0)
  • 2020-11-22 11:24

    I cam up with a recursive solution:

    def sumDigits(num):
    #   print "evaluating:", num
      if num < 10:
        return num
    
      # solution 1
    #   res = num/10
    #   rem = num%10
    #   print "res:", res, "rem:", rem
    #   return sumDigits(res+rem)
    
        # solution 2
      arr = [int(i) for i in str(num)]
      return sumDigits(sum(arr))
    
    # print(sumDigits(1))
    # print(sumDigits(49))
    print(sumDigits(439230))
    # print(sumDigits(439237))
    
    0 讨论(0)
  • 2020-11-22 11:25
    reduce(op.add,map(int,list(str(number))))
    

    Test:

    from datetime import datetime
    number=49263985629356279356927356923569976549123548126856926293658923658923658923658972365297865987236523786598236592386592386589236592365293865923876592385623987659238756239875692387659238756239875692856239856238563286598237592875498259826592356923659283756982375692835692385653418923564912354687123548712354827354827354823548723548235482735482354827354823548235482354823548235482735482735482735482354823548235489235648293548235492185348235481235482354823548235482354823548235482354823548234
    
    
    
    startTime = datetime.now()
    
    for _ in  range(0,100000) :
        out=reduce(op.add,map(int,list(str(number))))
    
    now=datetime.now()
    runningTime=(now - startTime)
    
    print ("Running time:%s" % runningTime)
    print(out)
    

    Running time:0:00:13.122560 2462

    0 讨论(0)
  • 2020-11-22 11:25

    It only works for three-digit numbers, but it works

    a = int(input())
    print(a // 100 + a // 10 % 10 + a % 10)
    
    0 讨论(0)
  • 2020-11-22 11:26

    Doing some Codecademy challenges I resolved this like:

    def digit_sum(n):
    arr = []
    nstr = str(n)
    for x in nstr:
        arr.append(int(x))
    return sum(arr)
    
    0 讨论(0)
提交回复
热议问题