function that counts...

Peter Pearson ppearson at nowhere.invalid
Thu May 20 12:06:33 EDT 2010


On Wed, 19 May 2010 21:58:04 +0200, superpollo <utente at esempio.net> wrote:
> ... how many positive integers less than n have digits that sum up to m:

If it's important for the function to execute quickly for large n,
you might get a useful speedup by testing only every ninth integer,
since any two integers whose digits add up to m differ by a multiple
of 9.  A simple application of this observation would be to test
range( m % 9, n, 9 ) rather than testing range( 1, n ).

I suspect that further applications of number theory would
provide additional, substantial speedups, but this wanders
away from the subject of Python.

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list