[Tutor] which way do I go?
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Wed Oct 15 18:04:18 EDT 2003
On Wed, 15 Oct 2003, Alan Gauld wrote:
> > >>>sumdifference (3, 9)
> >
> > my result should be 30
> > this comes from adding the numbers 4+5+6+7+8=30
>
> Look at how to define functions.
Time for more contradictory suggestions. *grin*
Simplifed problem: Can you write a program that sums the numbers from 0 to
9? How about a program that sums the numbers from 0 to 3?
More generally: Can you write a function that accepts an input 'n' and
sums the numbers from 0 to n?
This problem should be simpler for you, since the left endpoint is always
at zero. (It should also remind you of a problem you really should have
seen in algebra called "Gauss's sum".)
If you have a function that solves the simpler zero-based problem, you can
get the sum of an arbitrary interval in a very cute way.
Good luck to you.
More information about the Tutor
mailing list