[Tutor] finding factorials

Payal Rathod payal-python@staticky.com
Mon Jun 30 10:26:03 2003


Hi,
Thanks for the mails,

On Mon, Jun 30, 2003 at 11:14:36AM +0200, Kristoffer Erlandsson wrote:

> The method you are using now, when you are calling the function itself is
> called recursion. I would not recommend using recursion yet in a while,

Ok.

> it can be pretty hard to grasp. Try solving this problem using some kind
> of loop instead. A good start for making this loop for this problem would
> be something like:
> 
> while b != 0:

I have done it now, though I don't know whether the result is right
or not cos' I left maths when I was 15 years old and haven't touched
it since.
I have given my solution below, but still I am not able to get this using
functions. Can anyone give more hints?
I have given both working and non-working solution below. Check the comments.



#!/usr/local/bin/python
# Non-Working Solution
def euclid(a,b):
        while b != 0:
                c = a
                a = b
                b = c % a
                print 'A = ', a
                print 'B = ', b
        return a

x = 100
y = 5

euclid(x, y)
# This should perfrom the calculations an assign the common factor as 5.
# But this is giving it as 100.


print x
print y



#!/usr/local/bin/python
# Working Solution
a = 99
b = 15
print 'Original A =', a
print 'Original B =', b
print

while b != 0:
        c = a
        a = b
        b = c % a
        print 'A = ', a
        print 'B = ', b
print a


Thanks a lot and bye.
With warm regards,
-Payal




-- 
"Visit GNU/Linux Success Stories"
http://payal.staticky.com
Guest-Book Section Updated.