[Tutor] finding factorials - hmm..., gcd
Gregor Lingl
glingl@aon.at
Tue Jul 1 15:50:02 2003
Payal Rathod schrieb:
>On Tue, Jul 01, 2003 at 11:15:01AM +0100, glingl wrote:
>
>
>>Your code is ok and gives the right result!
>>e.g change: x, y = 25, 15 and you will get:
>>
>>
>
>Are you sure? I mean I don't get what a common factor means? Can you
>tell?
>
>
>
>># (1) normally it's not considered good practice to pu
>># print-statements in a function which aims at calculating
>># a numerical rsult. -
>>
>>
>
>
>Why??? I mean it is good for troubleshooting/debugging.
>
Yes! (But didn't I say that?)
>
>
>
>># (2) here you use an uncoditional return - so the body of
>># the while-loop will be exited after the first run.
>>
>>
>
>I don't get this. You mean to say that once I type return euclid(a,b),
>the while loop stops?
>
Not only the while-loop stops but the execution of the program leaves
the (code of the) function and continues with finishing the statement,
which contains
the function call - in your case an assignment, which assigns the returned
value to the variable result.
>Why?
>
This is the purpose the return-statement is intended for
>Also, if it does not get executed
>
I didn't say, that the body of the while loop doesn't get executed. I
said that it *invariably* is executed *only_once*, which is not was
a loop is normally meant for.
>how am I
>getting right answers as you say?
>
>
Because it does the same as the code of the recursive correction
(that is the second one) in my posting.
>
>
>># Consequently it will never work as a loop and you could
>># (a) use an if instead of while or
>>
>>
>
>Can you point a small example in *this* context using an if statement?
>
I think, I gave you two "small" examples exactly in *your_context*,
namely two different possible
modifications of your code -
(a) the first one resulting in a euclid-function using a while loop (BUT
NO RECURSION) and
(b) the second one using recursion (BUT NO WHILE-LOOP)
>># (b) drop the return and recursive call of euclid -
>># recursion also accomplishes some sort of iteration
>># and should/need/(must?) not be done in addition
>># to the while-iteration
>>
>>
>
>I didn't get your meaning on this too, please.
>
>
A recursive functions calls itself (again, several times, until the
stop-condition is reached), so
its code is executed repeatedly.
>># (3) here you can leave out the three statements
>>#
>># c = a
>># a = b
>># b = c % a
>>#
>># if think carefully about which values are to be
>># inserted as arguments in the recursive function
>># call.
>># What are the values of a and b respectively after
>># execution of these 3 statements. (Remember: ist is
>># perfectly right to insert arithmetic expressions like
>># a / b, a+b etc as arguments - in which case they
>># will be computed (evaluated( before the results are
>># "bound" to the parameters.)
>>
>>
I fear, my crude English is responsible, that you don't understand what I
mean. Although just this maybe really hard to grasp. I wont't give an
explanation, more gifted people exist on this list. Just try the following
code and think about it:
print "WHAT'S GOING ON?"
x =4
y=10
z = 0
print "x =",x,"y =",y,"z =",z
def look(a,b):
print "x =",x,"y =",y,"z =",z,"a =",a,"b =",b
a=2*a
b=2*b
c=a*b
print "x =",x,"y =",y,"z =",z,"a =",a,"b =",b,"c =",c
return c
c = c*c # ;-)
print "x =",x,"y =",y,"z =",z,"a =",a,"b =",b,"c =",c # or: :-( ?
print "x =",x,"y =",y,"z =",z
z = look(x,y) # this will execute 2 (!) print-statements
# you won't be able to print a,b,c here
print "x =",x,"y =",y,"z =",z
Regards, Gregor
>Ahh! Sorry again, I didn't get you. I don't understand much programmers
>jargon. The whole paragraph looks greek to me.
>
>
>
>>P.S.: Maybe http://www.ibiblio.org/obp/thinkCSpy/ ,
>>especially chapters 4 to 6,
>>contains material, which could be helpful for you.
>>
>>
>
>I had read them 2 days back. Maybe I should read them again.
>
>
>Thanks a lot for the time and patience.
>With warm regards,
>-Payal
>
>
>