Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Given $a, d, n, x$.

Suggest me a suitable algorithm to compute the number of times the digit $x$ appearing in the arithmetic sequence $a, a + d, a + 2 \times d, \cdots, a + n \times d$.

For example : If $ a= 10, d=5,n= 2,x = 0$ the sequence is ($10, 15,20$) so the answer is $2$

Domain specification: $- 1 \le a, n \le 10^9. \text{ and } - 1 \le d \le 20,000. $

share|cite|improve this question

I cannot tell you how best to go about this problem, but here is the approach I would use: The number of digits that occur is the sum of the number of digits that occur in the ones place, the tens place, etc. For the ones place, treat all these operations mod 10, and this becomes easy because n cycles from 0 to 9. For other places, I would suggest finding the mth from last digit by looking at the sequence of terms (nth term mod 10^m - nth term mod 10^(m-1))/10^m.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.