[Tutor] Add all natural numbers that are multiples of 3 and 5

Alan Gauld alan.gauld at btinternet.com
Sun Jan 4 18:57:16 CET 2009


"bob gailer" <bgailer at gmail.com> wrote

> Also consider that the sum of consecutive integers between 1 and n 
> is n*(n+1)/2.
> Calculate that for 1..333 then multiply by 3
> Calculate that for 1..199 then multiply by 5
> add those
> Calculate that for 1..66 then multiply by 15
> subtract that from the total

Ooh, that's sneaky! I haven't seen that before.
Took me a couple of reads through to figure out how it worked :-)


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk 




More information about the Tutor mailing list