Recursive Python function

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Recursive Python function

 
0
  #1
Apr 8th, 2009
Hello there,

I've a homework assignment from a while ago that asks
"Implement a recursive Python function which returns the sum of the first n integers"

Now i just dont know exactly what it's asking and how to get started.

Recursive functions, right (my thoughts so far is it'll look something like )

  1. def sum(n,amount):

and then i get stumped, i have worked with some so it usually continues with if (statement) == Integer etc.

But for this i seem to need to ask what the n integers are, then ask how many they want to add.

It's confused me, as my text wall probably has to you haha
Thanks for the help in advance, (try to keep it simple for me ^^)
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Re: Recursive Python function

 
0
  #2
Apr 8th, 2009
thinking about this more, should i define what the n numbers are and then just ask what the first amount of numbers to sum up are?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: Recursive Python function

 
1
  #3
Apr 8th, 2009
  1. def sum(n):

Maybe that will help you get on track. If you have worked with other recursive functions, you will find that this will be very similar and should be only a few lines of code.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 181
Reputation: adam1122 is an unknown quantity at this point 
Solved Threads: 28
adam1122 adam1122 is offline Offline
Junior Poster

Re: Recursive Python function

 
0
  #4
Apr 8th, 2009
Just as a follow up in case this is your confusion:
  1. sum(1) == 1 # 1
  2. sum(2) == 3 # 1+2
  3. ...
  4. sum(10) == 55 # 1+2+3+4+5+6+7+8+9+10
Last edited by adam1122; Apr 8th, 2009 at 8:12 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,042
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 933
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Recursive Python function

 
1
  #5
Apr 8th, 2009
Hint:
Recursive functions call themselves, basically form a loop

Your function definition should look like:
def summer(n, mysum=0):

You are summing from the top down. Each recursion you add n to mysum and decrease n by 1. So your recursive call becomes
return summer(n-1, mysum+n)

You loop until n drops to zero, then you return mysum. That's where an if/else is used.

Your initial function call is something like
mysum = summer(5)
if you want to sum up integers 1 to 5.
Last edited by vegaseat; Apr 8th, 2009 at 8:28 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 43
Reputation: thehivetyrant is an unknown quantity at this point 
Solved Threads: 0
thehivetyrant thehivetyrant is offline Offline
Light Poster

Re: Recursive Python function

 
0
  #6
Apr 8th, 2009
I believe i understand now and i have completed the task,
I thank both of you for your help, both ways were extremely helpful,
just having it wrote down simply.

I am very grateful.

I also have to do an iterative version of this but i'm sure i'll be able to handle it now that i understand.

Again thanks to both of you.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC