header_title("GCD Calculator (Euclid's Algorithm)", "GCD Calculator", "This cgi-bin form calculates the greatest common divisor of two positive " . "integers using " . "Euclid's classic algorithm."); } function do_footer(&$style) { $style->footer_std(); } function do_form($N1_refresh, $N2_refresh, $Buttontext) { echo "
\n"; echo "\n"; echo "\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "
\n"; echo "

N1 (arbitrary length):

\n"; echo "

N2 (arbitrary length):

\n"; echo "

\n"; echo "
\n"; } function do_err_msg($Err_msg) { echo "

"; echo $Err_msg; echo "

\n"; } //Main script begins here. // // $style = new Stdnwpstyle; //Assign the current style in force. Also, starts the CPU //usage clock. //Do the header unconditionally. The header is always used on this page. do_header($style); // // //If N1 was supplied, decommanate it and be sure it is a string. if (isset($N1)) Arbint_decommanate($N1); //Same for N2. if (isset($N2)) Arbint_decommanate($N2); // //There are a few cases to break into here, and this affects the overall layout of the page. //The header and footer are fixed, but the "guts" will change. if (!isset($N1) && !isset($N2)) { //In this case, we are probably visiting the form for the first time, and have not done a submit. //Just display the form itself. do_form("", "", "Calculate GCD"); } elseif (!isset($N1) && isset($N2)) { //Only $N2 is supplied. do_err_msg("You must supply both N1 and N2 to calculate a GCD.  Only N2 was supplied.  Please try again."); $style->hrule_std(); Arbint_commanate($N2); do_form("", $N2, "Calculate GCD"); } elseif (isset($N1) && !isset($N2)) { //Only $N1 is supplied. do_err_msg("You must supply both N1 and N2 to calculate a GCD.  Only N1 was supplied.  Please try again."); $style->hrule_std(); Arbint_commanate($N1); do_form($N1, "", "Calculate GCD"); } elseif (!Arbint_issimpleint($N1) || !Arbint_issimpleint($N2)) { //$N1 and $N2 are both supplied, but one or both is malformed. Need to advise. if (!Arbint_issimpleint($N1) && !Arbint_issimpleint($N2)) { do_err_msg("N1 and N2 are both malformed integers.  Choose new values and try again."); $style->hrule_std(); do_form($N1, $N2, "Calculate GCD"); } elseif (!Arbint_issimpleint($N1)) { do_err_msg("N1 is a malformed integer.  Choose a new value for N1 and try again."); $style->hrule_std(); Arbint_commanate($N2); do_form($N1, $N2, "Calculate GCD"); } else { do_err_msg("N2 is a malformed integer.  Choose a new value for N2 and try again."); $style->hrule_std(); Arbint_commanate($N1); do_form($N1, $N2, "Calculate GCD"); } } else { //N1 and N2 were both supplied and seem to be valid integers. // //If either integer is negative, make it positive. if (Substr($N1, 0, 1)=="-") $N1 = Substr($N1, 1, strlen($N1)-1); if (Substr($N2, 0, 1)=="-") $N2 = Substr($N2, 1, strlen($N2)-1); //If either is "0", make it "1". if ($N1=="0") $N1 = (string)"1"; if ($N2=="0") $N2 = (string)"1"; $cur_a = new Arbint; $cur_b = new Arbint; $cur_div = new Arbint; $cur_mod = new Arbint; $cur_a->assign($N1); $cur_b->assign($N2); //If b>a, then swap them. if (Arbint_cmp($cur_a, $cur_b) < 0) { $temp = $cur_a; $cur_a = $cur_b; $cur_b = $temp; } $round = 0; do { $round++; $cur_div->div($cur_a, $cur_b); $cur_mod->mod($cur_a, $cur_b); $a[$round] = $cur_a->get_digits(); Arbint_commanate($a[$round]); $b[$round] = $cur_b->get_digits(); Arbint_commanate($b[$round]); $adivb[$round] = $cur_div->get_digits(); Arbint_commanate($adivb[$round]); $amodb[$round] = $cur_mod->get_digits(); Arbint_commanate($amodb[$round]); $cur_a = $cur_b; $cur_b = $cur_mod; } while($amodb[$round] != 0); echo "

The greatest common divisor of "; echo $a[1]; echo " and "; echo $b[1]; echo " is "; if ($round == 1) echo $b[1]; else echo $amodb[$round - 1]; echo ".

\n"; $style->hrule_std(); echo "

Notes on the algorithm:  \n"; echo "Euclid's classic GCD algorithm was known no later than about 200 B.C. and is documented \n"; echo "at the \n"; echo "Wolfram Research site \n"; echo "and also in Knuth's classic reference work, "; echo "The \n"; echo "Art Of Computer Programming.  Euclid's GCD algorithm ends with the GCD being the \n"; echo "last non-zero remainder (in yellow below unless a is a multiple of b).\n"; echo "

\n"; //Assign the table column widths. They are centralized here for easy change. $width[0] = 5; $width[1] = 10; $width[2] = 10; $width[3] = 10; $width[4] = 10; echo "\n"; for ($i=0; $i<=$round; $i++) { echo "\n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo " \n"; echo "\n"; } echo "
\n"; if ($i==0) { echo "

Round

\n"; } else { echo "

" . $i . "

\n"; } echo "
\n"; if ($i==0) { echo "

a

\n"; } else { echo "

" . $a[$i] . "

\n"; } echo "
\n"; if ($i==0) { echo "

b

\n"; } else { echo "

" . $b[$i] . "

\n"; } echo "
\n"; if ($i==0) { echo "

a div b

\n"; } else { echo "

" . $adivb[$i] . "

\n"; } echo "
1 && ($i == ($round-1))) echo " bgcolor=\"#FFFF00\""; echo ">\n"; if ($i==0) { echo "

a mod b

\n"; } else { echo "

" . $amodb[$i] . "

\n"; } echo "

\n"; $style->hrule_std(); Arbint_commanate($N1); Arbint_commanate($N2); do_form($N1, $N2, "Calculate Another GCD"); } //Now do the footer unconditionally. The footer is always used on this web page. do_footer($style); ?>