Re: counting how many positive integers <n have digits that add up to m



superpollo wrote:

S Perryman ha scritto:

Daniel T. wrote:

My first thought is that there might be a formula that will return the result with no loops.

My first reply to you is :

What formula are you offering for computing the desired result .... ??

maybe some generating function magic ?

m=10, n = 12345 :

Smallest number is 19.
Break into 1 | 9.
There are (9 - 1) + 1 numbers < 100 (19, 28, ... 91) .

Somehow compute the next number.
x = 109 (19 + 90) .
Break into 10 | 9. Break into (10 mod 10) | 9 = 0 | 9.
There are (9 - 0) + 1 = 10 numbers < 200 (109, 118, ... 190) .

Compute the next number.
x = 208 (109 + 99) .
Break into 20 | 8. Break into (20 mod 10) | 8 = 0 | 8
There are (8 - 0) + 1 = 9 numbers less in 300.

And so on.


So there are variant conditions as to when to add 90 or 99 to
get the start of the next block of numbers.

But there are also some deviant cases. :-(
For example : x = 901.

Meaning 9 | 1.
1 - 9 + 1 does not equal 2 (901, 910) .

The next number is 1009.
901 + 90 = 991. 901 + 99 = 1000. Both are wrong.

However, 910 (the last number in the block) + 99 = 1009.


Regards,
Steven Perryman
.



Relevant Pages

  • Re: program
    ... begin followup to Joona I Palaste: ... > My version only used one-statement functions, with no loops or if ... Now if I could only somehow remove the need for the local ...
    (comp.lang.c)
  • Re: 2D convolution matrix with spatially varying kernels
    ... Like the others said, you'll have to do it by scratch with 4 nested "for" loops, unless you can somehow get your filter in nlfilter() to know how it's supposed to change as a function of position. ...
    (comp.soft-sys.matlab)
  • Re: Training Week Ending March 12, 2006
    ... Looking at the 10 hour finishers everyone slowed down ... If I could somehow manage to start out with ... But if you start out with 2 hour loops, you most likely won't be able to make up the difference late to break 10 hours. ... you will have seen the course and know where to push and where to back off as well as knowing if it's your day or not. ...
    (rec.running)
  • Re: non-separating 2-spheres in 3-manifolds
    ... somehow my answer was sent to somewhere else, ... theorems about loops which are embedded in the 3-mf and intersect the ... so probably I'm missing something here. ...
    (sci.math)