Answer the question
In order to leave comments, you need to log in
How to find the number of numbers from 1 to 10^9 that have the sum of the digits = x?
Given a single number X.
You need to find the number of numbers from 1 to 10^9 that have the sum of the digits of X.
For example, if X = 1, the answer will be 10.
I can’t find a pattern. With x = 1, everything is clear - we count the powers of ten and that's it, but what to do with other numbers?
Answer the question
In order to leave comments, you need to log in
I don't think there is any pattern. Just solve a problem that looks something like this: Find all sets of N numbers that give the number X, the first number is not zero. Having solved this problem, getting an answer for yours is very easy.
Another option is of course even simpler, just run through all the numbers and calculate the sum of the numbers.
The advantage of the first solution is that the 10^9 limit is easily overcome and you don't have to think about overflows and so on.
Head-on.
I sketched out the code in ruby, but already with a million, noticeable time is spent on the calculation. So it's better to use some compiling PL, then it will probably be faster.
10^9, ... For example, if X = 1, the answer is 10.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question