[Tutor] recursion sort of

Michael Farnham mfarnham@acm.org
Thu May 29 16:49:26 2003


On Thursday 29 May 2003 14:55, Jennifer Cianciolo wrote:
> Hi all,
>
> def run(n1, n2):
>     while n1>=501:
>         w=1
>         n1=n1*w

As Dana stated this is an infinite loop because
n1 does not change. If n1 starts out >= 501
then this loop is entered and it never exits.

>     while n1<=500:
>         w=0.2
>         n1=n1*W

Assuming that the capital W is a typo as suggested
by Dana, this is also an infinite loop. If n1 is <= 500
then this loop is entered and n1 is multiplied by 0.2.
Therefore n1 becomes smaller each time through the
loop and the loop is never exited.

>     while n2>=501:
>         w=0.8
>         n2=n2*w

This loop is OK. If n2 is >= 501 this loop is entered.
n2 is multiplied by 0.8, which makes n2 smaller. So
at some point n2 will be > 501 and the loop will be
exited.

>     while n2<=500:
>         w=0.4
>         n2=n2*W

Again assuming that the capital W is a typo this is
another infinite loop. This loop is entered when
n2 <= 500. It is then multiplied by 0.4 which makes
it smaller. Therefore n2 will never be > 500 and the
loop will never exit.

>     nt=n2+n1
>     print nt
>

I do not know what this algorithm is supposed to do
but this does not look right. For example suppose
that n2 = 600. The while n2 >= 501 loop is entered
and n2 is assigned 480 (0.8 * 600). Since n2 is now
<= 500 the second loop is also entered.

I would think that these two loops should be mutually
exclusive. Maybe they should not even be loops but
if then else constructs. However it is your algorithm
and you know it better than I do

HTH
Mike Farnham

-- 
You've got to be honest; if you can fake that, you've got it made.
                         -- George Burns