Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 15 Online
OpenStudy (anonymous):

Mind debugging this? I am trying to use the recursive method to sum all the multiples of 3 (to 100). call the method with (0, 0) and ignore the function description. It runs 'down' the chain but won't come back up. Code at: http://dpaste.com/551042/

OpenStudy (anonymous):

doesn't seem like you should need a while statement if it is recursive. are you practicing recursion?- there is an easier way. x = range(0,100,3) sum(x) here's what i came up with, but it gives the wrong answer http://dpaste.com/551492/

OpenStudy (anonymous):

this seems to work: http://dpaste.com/551494/

OpenStudy (surbhi):

@joz - the only mistake that i can figure out is with the while loop. just try out the code without the while statement ,its not needed. The output is coming out to be an infinite loop, right ? maybe it is becoz... suppose i has value 1. the while loop begins, its value gets incremented i.e now i is 2 and totsum is 0 and then the function multi3 is called ( in the while loop itself) . but remember the while condition (i<100) i.e the program control cannot go outside the loop unless i is >100. so maybe here... the computer just keeps on calling the funtion ( but not performing it ) and incrementing i till it is greater than 100. after i becomes greater than 100, the control comes out of the while loop into the if loop which becomes false so the else statement gets executed and a value might be shown on the output screen. But more funtion calls have been made ( about 100), so again the funtion would be executed which in turn wud give rise to more 100 function calls and so on....... and so u keep on getting endless values. I m not sure that this is the right answer to ur problem so it wud be better if u consult an expert. i hav just thought about it now. pls let me know if the code runs and the right explanation if this ones wrong.

OpenStudy (surbhi):

@ bwCa-u havn't understood the question well. u r checking tot <100 but that is not desired . the total can be anything. and even if that is desired then i guess it should give an endless loop , since in every return statement u call the multi3 function in which again the tot gets initialed to 0. u sure d code gives a value? pls let me know. i might be wrong since i don't know python . i hav done C++ recently ( in 11th and 12th class). in this question i hav just checked on the logic part.

OpenStudy (surbhi):

@joz i might be wrong since i don't know python . i hav done C++ recently ( in 11th and 12th class). in this question i hav just checked on the logic part.

OpenStudy (anonymous):

surbhi - my second post works and gives the correct value.. the tot<100 might look funny but it is happening on each recursion instance, the sum isn't calculated till all the recursions start returning... i could have given that variable a better name

OpenStudy (anonymous):

surbhi - tot = 0 in the function def is a default value - it is only 0 if you don't pass an argument

OpenStudy (surbhi):

btw r u getting 1683 as ur answer? i m sry but i m not getting ur explanation to how using tot<99 works. and i agree with u on the default argument part , i had forgotten it. thnks for reminding me.

OpenStudy (anonymous):

Okay... I had a chance to look through it better... took out the while loop, adding the function in the else... it works! but only if the max is 100. I tried 1000, and and it errors. The error is at about 976 iterations, it errors line 11 and 13, that is to say, the 2 options where the functions are. I let it run for quite a while, accidently, and finally it came up with the error 'RuntimeError: maximum recursion depth exceeded while calling a Python object'. So, perhaps this error is because after the errors run for a while, it cuts it out, but maybe it simply doesn't allow more than 976 functions to be looped through? This is the code: http://dpaste.com/552177/

OpenStudy (anonymous):

Okay... so I revised again... First off I want to thank you guys for your help, I appreciate it. I haven't had much of a chance to work on this over the last couple of days. Second, if you already read the old post, I'm rewriting it as my posts are getting too long. I was posing the question on whether the recursive method was suitable or not for this problem. I saw someone said they did it in the recursive method on Project Euler, and I thought, well, here is my chance to play with recursion. I think it is far easier using enumeration, and less iterations as it doesn't have to 'come back up'. If you haven't checked out Project Euler... do. I re-wrote the last effort, so that it doesn't have to go through the function unless it divides by 3, so cut down by a two thirds, and it works to 1000 fine. So I assume that was just too many recursions. http://dpaste.com/552179/ Also, I have checked out that up to 100 should be 1683 like you said, and 166833 for up to 1000. The default in the parameters is pretty cool.

OpenStudy (anonymous):

PS I had already completed the project in project euler I was doing, I just wanted to play with the recursion... PPS Surbhi, I'm not sure your analysis of the while loop is quite right, I think when it is called it goes into the next function, from the top, it's not stuck... but you were quite right that it was and should be unnessary in the context of recursive functions... well maybe I'll play with the while function in recursion when I have more time... or maybe I've had enough of this problem...

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!