| | |
Recursive Python function
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2009
Posts: 43
Reputation:
Solved Threads: 0
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 )
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 ^^)
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 )
Python Syntax (Toggle Plain Text)
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
hahaThanks for the help in advance, (try to keep it simple for me ^^)
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
Python Syntax (Toggle Plain Text)
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.
•
•
Join Date: Mar 2009
Posts: 181
Reputation:
Solved Threads: 28
Just as a follow up in case this is your confusion:
Python Syntax (Toggle Plain Text)
sum(1) == 1 # 1 sum(2) == 3 # 1+2 ... sum(10) == 55 # 1+2+3+4+5+6+7+8+9+10
Last edited by adam1122; Apr 8th, 2009 at 8:12 pm.
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.
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!
•
•
Join Date: Jan 2009
Posts: 43
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- iterative python (Python)
- Recursive function (Python)
- C to Python Translation anyone? (Python)
- module for recursive files? (Python)
- Return wont work in a function - why? (Python)
- pyramid function (Python)
- Loops in Python (Python)
- Sorting in Python (Python)
Other Threads in the Python Forum
- Previous Thread: lexers
- Next Thread: web python control transfer from one page to another
| Thread Tools | Search this Thread |
accessdenied advanced apache application argv beginner change command csv def dictionary dynamic edit enter event examples file float format function google gui homework import inches input jaunty java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric obexftp output parameters parsing path phonebook port prime programming projects py2exe pygame pygtk pyopengl python random recursion redirect remote return reverse scrolledtext session simple skinning smtp software sprite statictext stderr string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable voip windows wordgame wxpython






