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

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Greatest common divisor step by step in the Raku 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 Raku programming language

Source code in the raku programming language

sub gcd (Int $a is copy, Int $b is copy) {
   $a & $b == 0 and fail;
   ($a, $b) = ($b, $a % $b) while $b;
   return abs $a;
}

multi gcd (0,      0)      { fail }
multi gcd (Int $a, 0)      { abs $a }
multi gcd (Int $a, Int $b) { gcd $b, $a % $b }

my &gcd = { ($^a.abs, $^b.abs, * % * ... 0)[*-2] }

my $gcd = $a gcd $b;

[gcd] @list;         # reduce with gcd
@alist Zgcd @blist;  # lazy zip with gcd
@alist Xgcd @blist;  # lazy cross with gcd
@alist »gcd« @blist; # parallel gcd

  

You may also check:How to resolve the algorithm Evaluate binomial coefficients step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the AutoHotkey programming language
You may also check:How to resolve the algorithm Xiaolin Wu's line algorithm step by step in the Racket programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the Modula-3 programming language
You may also check:How to resolve the algorithm Extend your language step by step in the Coq programming language