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

Published on 12 May 2024 09:40 PM

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

Source code in the rascal programming language

public int gcd_iterative(int a, b){
	if(a == 0) return b;
	while(b != 0){
		if(a > b) a -= b;
		else b -= a;}
	return a;
}

rascal>gcd_iterative(1989, 867)
int: 51

public int gcd_recursive(int a, b){
	return (b == 0) ? a : gcd_recursive(b, a%b);
}

rascal>gcd_recursive(1989, 867)
int: 51

  

You may also check:How to resolve the algorithm Array concatenation step by step in the langur programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Scala programming language
You may also check:How to resolve the algorithm Pseudo-random numbers/Middle-square method step by step in the BASIC programming language
You may also check:How to resolve the algorithm LZW compression step by step in the Eiffel programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the D programming language