EnglishРусский  

   hello

   square

   easymath

   runini

   easyhtml

   calendar

   samefiles

   To be continued

Ads

Installer and installation software
Commercial and Freeware installers.

easymath

Example 1

Now we will analyze an example that finds the greatest common divisor of two numbers (GCD).

We take advantage of Euclid's Algorithm for the task solution. It works like this:

GCD( x, y ) = x if y equals 0 and
GCD( x, y ) = GCD( y, x MOD y ) if y is nonzero.

x MOD y is the remainder of values.
In other words, dividing two numbers we compute the remainder of the values but if the remainder is nonzero, the second number and the remainder of the values must be considered, etc.

The following fragment provides an obvious example of recursion, that's used to have a function call itself from within itself.
This function is written like this:

func uint gcd( uint first second )
{
   if !second : return first
   return gcd( second, first % second )
}

% is used to divide two numbers and returns the remainder.
uint is a type designating a positive integer.
if is a conditional statement shown in the following example:

if condition {
}
elif condition {
}
else {
}

Note: An infinite number of elif blocks can be used. If the condition is TRUE, statements in braces following this condition will be executed.

Finally, it is time to write the main function that can receive the data from the user and call gcd. The function contains the following loop:

while 1
{
   first = uint( congetstr( "Enter the first number ( enter 0 to exit ): ", input ))
   if !first : break
   second = uint( congetstr( "Enter the second number: ", input ))
   print("GCD = \( gcd( first, second ))\n\n")
}

congetstr is a function provided by standard libraries, it outputs a text to the screen and receives the data from the user.

Example 2

Calculate a factorial n! for n from 1 to 12. A factorial determines the product of numbers up to the given number inclusive.

The following program demonstrates its task solution:

func uint factorial( uint n )
{
   if n == 1 : return 1
   return n * factorial( n - 1 )
}

func main<main>
{
   uint    n
   
   print("This program calculates n! ( 1 * 2 *...* n ) for n from 1 to 12\n\n")
   fornum n = 1, 13
   {
      print("\(n)! = \(factorial( n ))\n")
   }
   getch()
}

The fornum loop is executed while the counter variable "n" is considered less than the value of the second expression. The loop counter increases by increments of 1 at each step. fornum is a special case of the for operator - more on that later.

for counter = expression,expression,change of the value of counter
{
}

Exercise 3

Now we need to calculate the Fibonacci numbers.

These are a series of whole numbers in which each number is the sum of the two preceding numbers. Beginning with 0 and 1, the sequence of Fibonacci numbers would be 0,1,1, 2, 3, 5, 8, 13, 21, 34, etc. using the formula: n = n(-1) + n(-2), where the n(-1) means "the last number before n in the series" and n(-2) refers to "the second last one before n in the series."

We will calculate until the last number exceeds 2000000000. Use a recursive function.
X0 = 1
X1 = 1
...
Xn = Xn-1 + Xn-2

Exercise 4

Perform the previous task without the use of recursion.

Source