A
A
Artem Zinkin2013-11-13 22:22:51
Programming
Artem Zinkin, 2013-11-13 22:22:51

Algorithm for counting the number of numbers between A and B whose sum of digits is even?

Hello.
I help schoolchildren to prepare for the Olympiad in programming. I'm going to solve some problems. The solution to one problem, in my opinion, is not rational. However, I can't find a better solution.
Condition of the problem: given two numbers A and B. Count the number of natural numbers on the segment from A to B, the sum of whose digits is even.
The program receives two natural numbers A and B, not exceeding 10^9, A <= B.
The program must output one number - the number of natural numbers greater than or equal to A and less than or equal to B, the sum of whose digits is even.
The solution that comes to mind: two loops (a loop within a loop) - In the first, we iterate over the numbers themselves, and in the second, the digits of each number.
It should be borne in mind that the guys have to write in a linear programming language (therefore, some solutions, in principle, cannot be optimal, but not so much)
I would like to get an idea how to implement this program without the second cycle (or maybe without the first - by mathematical calculations) or your thoughts on an algorithm for finding numbers whose sum of digits is even, without iterating over the numbers themselves (if possible).
By the way, it's strange that such numbers don't have names yet :)
Thank you in advance, Artem.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
Mrrl, 2013-11-14
@zinkinru

There are exactly 5 such numbers on each segment from 10*n to 10*n+9. Therefore, it is enough for us to count the number of such complete segments and process the edge segments. Let sumdig(n) be a function that returns the remainder after dividing the sum of n digits by 2. Then: int s0=(B/10-A/10-1)*5; int s1=(10+sumdig(A/10)-A%10)/2; int s2=(2+B%10-sumdig(B/10))/2; return s0+s1+s2;

A
alex1234, 2013-11-13
@alex1234

And if it is trite (1 + BA) / 2 (without a fractional part)? It seems to converge for small numbers :-)

T
TDK, 2013-11-13
@TDK

We take the number A, determine for it the parity of the sum of numbers, then we argue as follows: if A is odd, then A + 1 will be even, if A is even, then A + 1 is odd. We make an exception for the transition xx9 - xy0 (there, in my opinion, xx9 and xy0 have the same parity). In general, we alternately run to B. I would do this.

I
Igor, 2013-11-14
@leotop

Did I understand the terms of the problem correctly?

An arbitrary range is given, for example, from 2000 to 5000. For each number, for example, 2013, add the numbers, 2 + 0 + 1 + 3, and if the resulting number is even, increase the counter by 1?

those. frontal decision

1) Loop: created an array with data in the given range 2) Loop: Parsed the numbers into components, added, divided by two, determined whether it was even or not 3) If even, increased the counter by 1 4) Displayed the result at the end of the program

In this case, the mathematical solution without cycles will be the fastest, but is this a school level?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question