GCD Through Euclidean Algorithm

GCD is the greatest number which can divide any two numbers without a remainder.

 Here we are going to write a file named gcd.tcl and then we will run it as, tclsh gcd.tcl 

# gcd.tcl

#

proc gcd {first second} {

set a [set b 0];

if {$first >= $second} {

set a $first;

set b $second;

} else {

set a $second;

set b $first;

}

while {1} {

set m [expr {$a % $b}];

if {$m == 0} {

break;

}

set a $b;

set b $m;

}

return $b;

}

#Examples

#

puts "GCD of 633 and 142 is: [gcd 633 142]";

puts "GCD of 12 and 42 is: [gcd 12 42]";


Happy reading and thank you. Meet you soon.

Comments

Popular posts from this blog

Bash Shell In Linux

Creating An Animation Using A Text Editor

expr command In TCL