How to resolve the algorithm Greatest common divisor step by step in the MiniZinc programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greatest common divisor step by step in the MiniZinc programming language

Table of Contents

Problem Statement

Find the greatest common divisor   (GCD)   of two integers.

Greatest common divisor   is also known as   greatest common factor (gcf)   and   greatest common measure.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Greatest common divisor step by step in the MiniZinc programming language

Source code in the minizinc programming language

function var int: gcd(int:a2,int:b2) =
  let {
    int:a1 = max(a2,b2);
    int:b1 = min(a2,b2);
    array[0..a1,0..b1] of var int: gcd;
    constraint forall(a in 0..a1)(
      forall(b in 0..b1)(
        gcd[a,b] ==
        if (b == 0) then
          a
        else
          gcd[b, a mod b]
        endif
      )
    )
  } in gcd[a1,b1];  
 
var int: gcd1 = gcd(8,12);
solve satisfy;
output [show(gcd1),"\n"];

  

You may also check:How to resolve the algorithm Runge-Kutta method step by step in the Dart programming language
You may also check:How to resolve the algorithm Roots of unity step by step in the GAP programming language
You may also check:How to resolve the algorithm Heronian triangles step by step in the EchoLisp programming language
You may also check:How to resolve the algorithm Increasing gaps between consecutive Niven numbers step by step in the REXX programming language
You may also check:How to resolve the algorithm String prepend step by step in the EMal programming language