2

Trying to figure out how to find the sum of digits between two numbers(including those numbers) using a function in python.

I tried recursion for each individual argument and then subtracted them to compensate for the numbers in between. Then I added these two and got my sum, which is incorrect for every argument unless the digits are below 10. Not sure of the proper way to approach this problem, please help.

def sum_digits(a, b):
"""sum of digits between two numbers"""
  sum = 0
  ones = a - b
  if ones < 0:
    ones = ones * -1


  if a >= 10 and b >= 10:
    sum += ones


  while a > 0 and b > 0:
    d = a % 10 + b % 10
    a = a // 10
    b = b // 10
    sum += d

  return sum
Lasaro Morell
  • 23
  • 1
  • 3

7 Answers7

0

Does this work ?

this works basically by getting the numbers within a range. Now since endrange is usually one less, I manually add a 1 to the endrange

startNumber = 1
endNumber = 5
total = 0;
for i in range(startNumber,endNumber+1):
    print(i)
    total += i

print total

Thanks

Gagan
  • 5,416
  • 13
  • 58
  • 86
  • 1
    Thanks for helping out! Sorry for my poor explanation, but I mean adding the individual numbers between the range. So say you have the argument (17, 20) you would want (1 +7)+(1+ 8)+(1+9)+(2 + 0) which equals 29. I don't understand how to approach this problem at all. – Lasaro Morell Apr 13 '19 at 23:17
0
def sum_digits(a, b):
    sum = 0
    for i in range(a,b+1):
        for e in (str(i)):
            sum += int(e)
    return sum

print(sum_digits(17, 20))
Rob Kwasowski
  • 2,690
  • 3
  • 13
  • 32
0

Just sum the digit sum for every number from the first argument to the last. For more ways to do each digit sum, see Sum the digits of a number - python

def sum_digits(a, b):
    total = 0
    for number in range(a,b+1):
        total += sum(int(digit) for digit in str(number))
    return total
Heath Raftery
  • 3,643
  • 17
  • 34
0

My approach:

def sum_of_products(lst, s, f):
    result = 0
    for i, item in enumerate(range(s, f+1)):
        lst[i] = list(map(int, str(item)))
        result += sum(lst[i])
    return result

lst = [x for x in range(0, 10)]
x = sum_of_products(lst, 14, 20)
print(x)
Duck Ling
  • 1,577
  • 13
  • 20
0

Here you go:

def sum_digits(a, b):
    sum = 0
    for i in range(a, b + 1):
        number = i
        while (number > 0):
            sum += number % 10
            number = number // 10
    return sum

print(sum_digits(17, 20))
Kacper Dębowski
  • 160
  • 1
  • 13
0

My 2 cents would to point out that there should be a closed-form formula that doesn't involve looping trough the whole range. For example, we know that the sum of n numbers is

n*(n-1)/2

and for the sum of digits 0 to 9 it is 45 == 9*10/2

01
02
03
04
05
06
07
08
09

then it becomes a bit more complicated for the next 10 numbers:

10
11
12
13
14
15
16
17
18
19

the sum is 10 times the tens(decades) plus 45. and then we could have:

for 00..09 we have 0*10+45
for 10..19 we have 1*10+45
for 20..29 we have 2*10+45
...
for d0..d9 we have d*10+45

I am too lazy to derive the good formula myself, therefore I Googled it. And below is what I have found:

The formula is simple if we know before hand the number of digits. For example, as per https://oeis.org/A007953 , if the number of n is less than 100 then the closed-form formula is:

   For n < 100 equal to (floor(n/10) + n mod 10) 

For an arbitrarily large number there is a sample code here: https://www.geeksforgeeks.org/count-sum-of-digits-in-numbers-from-1-to-n/

dsum(10**d - 1) = dsum(10**(d-1) - 1) * 10 + 45*10**(d-1) 

to compute the digit sum of a range just find the difference

dsum(b) - dsum(a)
Dr Phil
  • 833
  • 6
  • 18
0
def add_between_numbers(num1:int,num2:int):
    sum = num1 + num2
    for i in range(num1 - num2):
        if i >=1:
            sum += (num2 + i)
    print(sum)
num1 = int(input("Enter number 1: "))
num2 = int(input("Enter number 2: "))
if num1>num2:
    add_between_numbers(num1,num2)
else:
    add_between_numbers(num2,num1)

Write this code and you would be good to go.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
  • 1
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Mar 02 '23 at 07:57
  • I think the check for num1 > num2 should happen inside the function. It's not immediately clear that num1 has to be less than num2 to call the function. – General Grievance Mar 02 '23 at 14:20