ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-Tutor
python-Tutor
Re: [Tutor] finding factorials
by Payal Rathod other posts by this author
Jun 30 2003 2:17PM messages near this date
Re: [Tutor] finding factorials | Re: [Tutor] finding factorials
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.

_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor
Thread:
Payal Rathod
lonetwin
Kristoffer Erlandsson
Payal Rathod
Gregor Lingl
Danny Yoo
Payal Rathod
Danny Yoo
Gregor Lingl
Danny Yoo

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved