| Programming language | Starting point of a program |
|---|---|
| Java | main() method |
| C/C++ | main() method |
| Python | First line in the program |
|
|
As long as one of the number is not zero (0) do
{
if ( number on A ≥ number on B )
replace the number on A by the value (A - B)
otherwise
replace the number on B by the value (B - A)
}
The Greatest Common Divisor (GCD) = B
|
We saw the execution of the Euclid algorithm using 2 pieces of paper: click here
public class Euclid
{
public static void main(String args[])
{
int A; // Memory cell named "A"
int B; // Memory cell named "B"
// These memory cells are like the 2 pieces of paper
// we used above. They can store and recall a value
A = 28; // Write "28" on the piece of paper named "A"
B = 36; // Write "36" on the piece of paper named "B"
// ================================
// This is the Euclid Algorithm:
// ================================
while ( A != 0 && B != 0 )
{
if ( A >= B )
A = A - B; // Replace the number on A by (A-B)
else
B = B - A; // Replace the number on B by (B-A)
}
System.out.println("GCD = " + B);
}
}
|
When the Java program is run on a computer, the computer will be doing pretty much the same thing as the execution on 2 pieces of paper that we saw here: click here
|
|
|
|
(Don't worry, the syntax rules are very simple; still, you need to remember them well !)