I have some question..
Operation Flashpoint Dragon Rising OFDR Forums :: OPERATION FLASHPOINT: DRAGON RISING :: OFDR EDITING :: EDITING Q+A
Page 1 of 1
I have some question..
I would like to touch on one issue regarding the Lua code, namely the calculation of the factorial of a given number.
Although recalling the function (recursion) in the editor does not work, it is necessary for a general understanding. This is our case, let's analyze it.
It is well known that the factorial of a number is calculated by the formula : n! = n * ( n  1 ) * ... * 1
That is, to calculate, for example, the factorial of the number 5 it would be : 5! = 5*4*3*2*1 = 120
in the lua syntax it looks like this :
At first glance, everything is simple: calculation from a given number with subtraction of one and subsequent repetition of the action until the specified number reaches zero.
Ok, to better understand the situation, let's change some parameters.
Change the name of the function and the name of the variable for it.
It turned out that the line c = io.read("*n") has a reference to a reserved variable that cannot be changed and contains the final result.
Now can see exactly how the calculation takes place.
If we take the number 5, then the first calculation is as follows: 5 * (51), then the "return" command starts the function again with a new variable, and the result is entered into a hidden variable, referenced by the line c = io.read("*n"). Let's label this hidden variable as _n
Let's summarize this total event as "recursion 1" : _n = b * ( b  1)  calculated the first value of the _n variable
According to the formula (5! = 5*4*3*2*1 = 120) we multiplied 5 by 4; further, we must multiply this amount by 3, then by 2 and 1.
That is, the calculation of the following recursions proceeds as : _n = _n * ( b 1)
But there is one catch: the following recursion, the result of which is recorded in the _n variable, contains the variable "b" equal to 3.
But if we look at the function, it clearly states that all consecutive numbers starting from a given first must go through a full cycle, that is:
"recursion 1" contains the variable 5
"recursion 2" contains the variable 4
"recursion 3" contains the variable 3
"recursion 4" contains the variable 2
"recursion 5" contains the variable 1
But actually happens is this:
"recursion 1" contains the variable 5

"recursion 3" contains the variable 3
"recursion 4" contains the variable 2
"recursion 5" contains the variable 1
The question is, what happens during "recursion 2"?
Although recalling the function (recursion) in the editor does not work, it is necessary for a general understanding. This is our case, let's analyze it.
It is well known that the factorial of a number is calculated by the formula : n! = n * ( n  1 ) * ... * 1
That is, to calculate, for example, the factorial of the number 5 it would be : 5! = 5*4*3*2*1 = 120
in the lua syntax it looks like this :
 Code:
function fact (n)
if n == 0 then
return 1  if n = 0, return 1 and exit the function
else
return n * fact(n1)  direct calculation of the current
 operation and its repetition
end
end
print("enter a number:")  displays a tool tip for the user
a = io.read("*n")
print(fact(a))  output of the result of the variable "a"
At first glance, everything is simple: calculation from a given number with subtraction of one and subsequent repetition of the action until the specified number reaches zero.
Ok, to better understand the situation, let's change some parameters.
Change the name of the function and the name of the variable for it.
 Code:
function std (b)
if b == 0 then
return 1
else
return b * std(b1)
end
end
print("enter a number:")
с = io.read("*n")
print(fact(с))
It turned out that the line c = io.read("*n") has a reference to a reserved variable that cannot be changed and contains the final result.
Now can see exactly how the calculation takes place.
If we take the number 5, then the first calculation is as follows: 5 * (51), then the "return" command starts the function again with a new variable, and the result is entered into a hidden variable, referenced by the line c = io.read("*n"). Let's label this hidden variable as _n
Let's summarize this total event as "recursion 1" : _n = b * ( b  1)  calculated the first value of the _n variable
According to the formula (5! = 5*4*3*2*1 = 120) we multiplied 5 by 4; further, we must multiply this amount by 3, then by 2 and 1.
That is, the calculation of the following recursions proceeds as : _n = _n * ( b 1)
But there is one catch: the following recursion, the result of which is recorded in the _n variable, contains the variable "b" equal to 3.
But if we look at the function, it clearly states that all consecutive numbers starting from a given first must go through a full cycle, that is:
"recursion 1" contains the variable 5
"recursion 2" contains the variable 4
"recursion 3" contains the variable 3
"recursion 4" contains the variable 2
"recursion 5" contains the variable 1
But actually happens is this:
"recursion 1" contains the variable 5

"recursion 3" contains the variable 3
"recursion 4" contains the variable 2
"recursion 5" contains the variable 1
The question is, what happens during "recursion 2"?
Razer01 Points : 13
Reputation : 1
Join date : 20141215
Operation Flashpoint Dragon Rising OFDR Forums :: OPERATION FLASHPOINT: DRAGON RISING :: OFDR EDITING :: EDITING Q+A
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum

