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



"Willem" <willem@xxxxxxxxxxxxxxx> wrote in message news:slrnhvd87i.h39.willem@xxxxxxxxxxxxxxxxxx
| robin wrote:
| ) "superpollo" <utente@xxxxxxxxxxx> wrote in message news:4bf44372$0$31380$4fafbaef@xxxxxxxxxxxxxxxxxxxxxx
| )| in python:
| )|
| )| def prttn(m, n):
| )| tot = 0
| )| for i in range(n):
| )| s = str(i)
| )| sum = 0
| )| for j in range(len(s)):
| )| sum += int(s[j])
| )| if sum == m:
| )| tot += 1
| )| return tot
| )|
| )| any suggestion for improvement?
| )
| ) Looks like your code finds the number of integers <= n, not < n.
| )
| ) There are three loops in your code, one of which is implied.
|
| Where ? I don't see it.

I did say that it was "implied".

| Or do you mean the number-to-string conversion ?

The number-to-string conversion is the implied loop.


.