Initializing help system before first use

Largest common divisor


Type: Programming
Rating: 2 (easy-medium)
Description: Find the largest common divisor of two integer numbers
  • recursive function calls
  • if-then-elif-then-else statement
  • alternative formulation using a 'while' loop
File(s): lcdiv.mos


lcdiv.mos
(!*******************************************************
  * Mosel Example Problems                              *
  * ======================                              *
  *                                                     *
  * file lcdiv.mos                                      *
  * ```````'``````                                      *
  * Example for the use of the Mosel language           *
  * (Largest common divisor of two numbers)             *
  *                                                     *
  * (c) 2008 Fair Isaac Corporation                     *
  *     author: S. Heipcke, 2001                        *
  *******************************************************!)

model Lcdiv                    ! Start a new model

function lcdiv(A,B:integer):integer
 if(A=B) then
  returned:=A
 elif(A>B) then
  returned:=lcdiv(B,A-B)
 else
  returned:=lcdiv(A,B-A)
 end-if   
end-function

declarations
 A,B: integer                  ! Two integer numbers
end-declarations

 write("Enter two integer numbers:\n  A: ")
 fflush
 readln(A)
 write("  B: ")
 fflush
 readln(B)

 writeln("Largest common divisor: ", lcdiv(A,B))

(!
 Instead of using a recursive function call, it is also possible to 
 formulate this program as a while loop:
  
 while (A <> B) do
  if (A>B) then 
   A:=A-B 
  else B:=B-A 
  end-if
 end-do
 
 writeln("Largest common divisor: ", A) 
!)

end-model

© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.