function that counts...

superpollo utente at esempio.net
Thu May 20 12:33:17 EDT 2010


Peter Pearson ha scritto:
> 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.
> 

great suggestion, thanks!




More information about the Python-list mailing list