Take the 2-minute tour ×
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.

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|improve this question
add comment

1 Answer

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|improve this answer
add comment

Your Answer

 
discard

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.