Sum of difference of even and odd digits in an integer

817 Views Asked by At

So I have an increasing sequence of numbers from 2 to N (any integer > 1).

For N = 300

2,3,4,5, ..., 297,298,299,300

I have to calculate the sum of absolute difference between even and odd digits (not indexes) for each integer in a given sequence.

For example:

  1. 2344798 its will be |(2+4+4+8) - (3+7+9)| = 5
  2. 28 = |(2+8) - 0| = 10
  3. 1002 = |(2+0+0)-1| = 1

I am trying to come up with some formula but I not able to.

Adding more: For N = 300 sequence will be:

2,3,4, ..., 298, 299, 300

Answer: |2-0| + |0-3| + |4-0| + .....+ |(2+8)-9| + |2 -(9+9)| + |0 - 3|

1

There are 1 best solutions below

0
On

This is a tricky function, I doubt there's a simple formula. Some intriguing patterns arise if you look at $n$ mod 9, although it's easier to see those patterns using mod 18. The cumulative sum over a sequence of integers may be a little more well-behaved; it would be fairly easy to calculate if it weren't for that pesky absolute value.

FWIW, here's a table of values, using rows of 18 that I created using Python.

      0  1  2  3  4  5  6  7  8  0  1  2  3  4  5  6  7  8
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  0:  0  1  2  3  4  5  6  7  8  9  1  2  1  4  3  6  5  8 
 18:  7 10  2  1  4  1  6  3  8  5 10  7  3  4  1  6  1  8 
 36:  3 10  5 12  4  3  6  1  8  1 10  3 12  5  5  6  3  8 
 54:  1 10  1 12  3 14  6  5  8  3 10  1 12  1 14  3  7  8 
 72:  5 10  3 12  1 14  1 16  8  7 10  5 12  3 14  1 16  1 
 90:  9 10  7 12  5 14  3 16  1 18  1  2  1  4  3  6  5  8 
108:  7 10  2  3  0  5  2  7  4  9  6 11  1  0  3  2  5  4 
126:  7  6  9  8  4  5  2  7  0  9  2 11  4 13  3  2  5  0 
144:  7  2  9  4 11  6  6  7  4  9  2 11  0 13  2 15  5  4 
162:  7  2  9  0 11  2 13  4  8  9  6 11  4 13  2 15  0 17 
180:  7  6  9  4 11  2 13  0 15  2 10 11  8 13  6 15  4 17 
198:  2 19  2  1  4  1  6  3  8  5 10  7  1  0  3  2  5  4 
216:  7  6  9  8  4  3  6  1  8  1 10  3 12  5  1  2  1  4 
234:  3  6  5  8  7 10  6  5  8  3 10  1 12  1 14  3  3  4 
252:  1  6  1  8  3 10  5 12  8  7 10  5 12  3 14  1 16  1 
270:  5  6  3  8  1 10  1 12  3 14 10  9 12  7 14  5 16  3 
288: 18  1  7  8  5 10  3 12  1 14  1 16  3  4  1  6  1  8 
306:  3 10  5 12  4  5  2  7  0  9  2 11  4 13  1  2  1  4 
324:  3  6  5  8  7 10  6  7  4  9  2 11  0 13  2 15  1  0 
342:  3  2  5  4  7  6  9  8  8  9  6 11  4 13  2 15  0 17 
360:  3  2  5  0  7  2  9  4 11  6 10 11  8 13  6 15  4 17 
378:  2 19  5  4  7  2  9  0 11  2 13  4 12 13 10 15  8 17 
396:  6 19  4 21  4  3  6  1  8  1 10  3 12  5  3  2  5  0 
414:  7  2  9  4 11  6  6  5  8  3 10  1 12  1 14  3  1  0 
432:  3  2  5  4  7  6  9  8  8  7 10  5 12  3 14  1 16  1 
450:  1  2  1  4  3  6  5  8  7 10 10  9 12  7 14  5 16  3 
468: 18  1  3  4  1  6  1  8  3 10  5 12 12 11 14  9 16  7 
486: 18  5 20  3  5  6  3  8  1 10  1 12  3 14  5  6  3  8 
504:  1 10  1 12  3 14  6  7  4  9  2 11  0 13  2 15  3  4 
522:  1  6  1  8  3 10  5 12  8  9  6 11  4 13  2 15  0 17 
540:  1  2  1  4  3  6  5  8  7 10 10 11  8 13  6 15  4 17 
558:  2 19  1  0  3  2  5  4  7  6  9  8 12 13 10 15  8 17 
576:  6 19  4 21  3  2  5  0  7  2  9  4 11  6 14 15 12 17 
594: 10 19  8 21  6 23  6  5  8  3 10  1 12  1 14  3  5  4 
612:  7  2  9  0 11  2 13  4  8  7 10  5 12  3 14  1 16  1 

And here's the Python code I used.

print(' ' * 5 + ' '.join(['{:2}'.format(i % 9) for i in range(18)]))
print(30 * '- ', end='')
for n in range(630):
    if n % 18 == 0:
        print('\n{:3}'.format(n), end=': ')
    x = abs(sum(i * ((i % 2) * 2 - 1) for i in map(int, str(n))))
    print('{:2}'.format(x), end=' ', flush=True)
print()