Decorator function using Example 1

Title: Package function calculates the number of prime numbers is between 2 and 100, returns the result

DEF F1 (F): # define an outer function
DEF F2 (): # a function defined in the outer function (this function is mainly to achieve the required package function), as specified in 2 to 100, it is not necessary parameter
SUM = 0 # later by + = 1 to the number of statistical prime number SUM
for I in Range (2, 101): # number of from 2 to 100 fetch, back one by one is determined whether or not a prime number
for j in Range (2, i): # this loop from 2 to i-1 one by one access, if there is a number divisible by i, the i is not a prime number, if j takes into i- 1, a i is only divisible by itself and 1
iF i% j == 0:
BREAK
elif j == i-1:
SUM = 1 + # j when i-1 are taken to the further number i is not found divisible then i is a prime number, for accumulating here. 1 + SUM
return F (+ SUM. 1) # because when i = 2, range (2,2) is empty, the cycle can not be immediately terminated, so the drain 2 out, so the sum + 1 2 will make it up
F2 return () # when calling f1, by returning
# @ F1 # is the meaning of the phrase by f1 () function decorative f () function means, for ease of understanding, here commented
DEF F (P): 
Print ( "There are% d primes within 2 ~ 100"% P)
f1 (F)

"" "
(. 1) calls the f1 function by the statement f1 (f), attention f1 (F) is a function operation expression, he needs to perform f1 () function, a value obtained return f2 (), so that the value F2 ()
(2) F2 () is a function of the inner operational expression, it also has value, it the value of f (sum + 1), this is still a function operation expression, what its value is ???
(3) in the inner function, we have come to a calculation by the statement sum + 1 = 25, that is, said f1 (f) the final return value is f (25), it still was a function expression, continue to call the function calculation
(4) perform the function f to get the final result >> there are 25 prime numbers 2 to 100
"" "

 

 

Guess you like

Origin www.cnblogs.com/liulian999/p/11964774.html