English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Here's a complete set of codes:

from math import *

def f(a,b):
if a > b:
return 0.0
else:
return 1/(a*(a + 2)) + f(a+4, b)

if __name__ == '__main__':
print "result=%10.6f" % f(1.0, 12.0)

What does " return 1/(a*(a + 2)) + f(a+4, b) " mean ? How do I add "1/(a*(a + 2)) " to "f(a+4, b) "?

2007-11-04 20:06:21 · 2 answers · asked by H 2 in Computers & Internet Programming & Design

2 answers

Let's look at the return line in question, and work backwards from there to find out exactly what's going on.

return 1/(a*(a + 2)) + f(a+4, b)

This means that when a is not greater than b, you should exit the function and return the following value back to its calling function:

1/(a*(a + 2)) + f(a+4, b)

Now, what does this value mean? a and b are given to the function "f" by parameter passing, and you can check what types they are by reading where the function is called. Under the "if __name__ == '__main__':" line, you'll see that the function "f" is being called like this: "f(1.0,12.0)". This corresponds to a=1.0 and b=12.0. Good, we have values for a and b now.

So back to that return value. a and b are floating point numbers. The first half of the return looks like this:

1/(a*(a + 2))

This is simple arithmetic. Working out the denominator and simplifying, we get:

1/(1.0*(1.0+2)) or 1/(1.0*(3.0)) or 1/(3.0) or 1/3.0

Remember that a is a floating point number, so 1/3 will yield .333 repeating and NOT 0, as would be returned from the integer division of 1 and 3.

This value is added to the second part of the return. Artithmetically, we can express it like this:

1/3.0 + f(a+4,b)

Remember the a > b "if" condition? a is now 5.0 and b remains 12.0. A will continually be incremented by 4 *until* a is greater than b, upon which case 0.0 will be returned.

As the other answerer noted, this is a recursive function because it contains a terminating condition and a call to itself. So "f" is called until a is greater than b, and all of the return values are summed.

You can actually add a number (like "1/3.0") to a function call (like "f(a+4,b)") because the function "f" returns some value that can be added with a plus sign (+) to a float.

Let's make our own function call to "f" with better parameter values so we can more easily see what is going on...

f(10.0,5.0)

This would make a=10.0 and b=5.0. When "f" is called, the first line is

if a>b:

Now, ask yourself if a is greater than b. Of course. 10 > 5. So the value 0.0 is returned.


Let's make it a little harder, where a is not greater than b.

f(6.0,8.0)

So, a = 6.0, b=8.0. Is a > b? No. So the "else" condition is reached and now we look at the body of the "else".

return 1/(a*(a + 2)) + f(a+4, b)

We're back here.... We can evaluate the first part just like we did earlier:

1/(6.0*(6.0+2)) or 1/(6.0*(8.0)) or 1/(48.0) or 1/48.0

We'll leave the answer in fraction form to avoid decimals.

Now on to the second part...

f(6.0+4,8.0)

This is a function call, just like any other. So we call "f" (now we're calling it from itself -- recursively).

Our new a and b values are now 10.0 and 8.0, taken from the parameters.

Is a > b? YES. 10 > 8. So 0.0 is returned and we leave the function immediately, not even looking at the "else" body.

Substitute 0.0 for the second part of the "return" line, and we get this:

1/48.0 + 0.0 --- which simply equals 1/48.0.



The original problem made more calls to "f" because a was farther than 4 numbers from b.


Also, on an irrelevant note, you don't actually need the math import on the first line. You are never using a function from the math file, so it is not needed. Yes, you are using addition,division, and multiplication, but this is built-in.

Hope this helps.

2007-11-06 05:55:25 · answer #1 · answered by Ricky V 2 · 1 0

It is a simple recursion; and it performs an algebraic formula as stated, it looks like some kind of geometric series.

Result is 0.11 about that, I wouldn't know the name of this.

2007-11-05 04:50:55 · answer #2 · answered by Andy T 7 · 0 0

fedest.com, questions and answers