28 |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29 |
//SOFTWARE. |
//SOFTWARE. |
30 |
//******************************************************************************** |
//******************************************************************************** |
31 |
//This module is the GCD subfunction of a general-purpose CGI-BIN program to support |
//This module is the GCD subfunction of a general-purpose CGI-BIN program to support |
32 |
//number theory demonstration applications. |
//number theory demonstration applications. |
33 |
// |
// |
34 |
//INPUT PARAMETERS |
//INPUT PARAMETERS |
35 |
//---------------- |
//---------------- |
52 |
// EQ : Other or unspecified error. |
// EQ : Other or unspecified error. |
53 |
// |
// |
54 |
//[02] The fully normalized first integer entered. This means it has been |
//[02] The fully normalized first integer entered. This means it has been |
55 |
// stripped of all weird characters, etc. This can be used by the |
// stripped of all weird characters, etc. This can be used by the |
56 |
// PHP script to repopulate the form boxes. |
// PHP script to repopulate the form boxes. |
57 |
//[03] The fully normalized second integer entered. |
//[03] The fully normalized second integer entered. |
58 |
// |
// |
75 |
//this program will contain a number of lines which is a multiple of 5. |
//this program will contain a number of lines which is a multiple of 5. |
76 |
// |
// |
77 |
//The return value (exit code) from this subfunction is always 0. |
//The return value (exit code) from this subfunction is always 0. |
78 |
// |
// |
79 |
|
|
80 |
#define MODULE_SUBFUNC_GCD |
#define MODULE_SUBFUNC_GCD |
81 |
|
|
109 |
return(0); |
return(0); |
110 |
} |
} |
111 |
|
|
112 |
//Copy the command-line arguments to a safe place where we can manipulate them. |
//Copy the command-line arguments to a safe place where we can manipulate them. |
113 |
//Leave 2 characters of space in case we assign a "0". |
//Leave 2 characters of space in case we assign a "0". |
114 |
arg1 = (char *)malloc((AUXFUNCS_size_t_max(1, strlen(argv[2])) + 1) * sizeof(char)); |
arg1 = (char *)malloc((AUXFUNCS_size_t_max(1, strlen(argv[2])) + 1) * sizeof(char)); |
115 |
arg2 = (char *)malloc((AUXFUNCS_size_t_max(1, strlen(argv[3])) + 1) * sizeof(char)); |
arg2 = (char *)malloc((AUXFUNCS_size_t_max(1, strlen(argv[3])) + 1) * sizeof(char)); |
167 |
//We assume at this point that we will be successful. Output |
//We assume at this point that we will be successful. Output |
168 |
//the header stuff. |
//the header stuff. |
169 |
printf("S\n"); |
printf("S\n"); |
170 |
mpz_out_str(stdout, 10, alg_a); |
mpz_out_str(stdout, 10, alg_a); |
171 |
printf("\n"); |
printf("\n"); |
172 |
mpz_out_str(stdout, 10, alg_b); |
mpz_out_str(stdout, 10, alg_b); |
173 |
printf("\n"); |
printf("\n"); |
174 |
|
|
175 |
//We require as an initial condition that a >= b. Make |
//We require as an initial condition that a >= b. Make |
176 |
//that happen. |
//that happen. |
190 |
{ |
{ |
191 |
//We are at next round. |
//We are at next round. |
192 |
round++; |
round++; |
193 |
|
|
194 |
//Values for this round inherited from the last one. |
//Values for this round inherited from the last one. |
195 |
mpz_set(alg_a, alg_b); |
mpz_set(alg_a, alg_b); |
196 |
mpz_set(alg_b, alg_amodb); |
mpz_set(alg_b, alg_amodb); |
197 |
|
|
198 |
//Calculate the quotient and remainder. |
//Calculate the quotient and remainder. |
199 |
mpz_fdiv_qr(alg_adivb, alg_amodb, alg_a, alg_b); |
mpz_fdiv_qr(alg_adivb, alg_amodb, alg_a, alg_b); |
200 |
|
|
201 |
//Output all the data from the round. |
//Output all the data from the round. |
202 |
printf("%d\n", round); |
printf("%d\n", round); |
203 |
mpz_out_str(stdout, 10, alg_a); |
mpz_out_str(stdout, 10, alg_a); |
204 |
printf("\n"); |
printf("\n"); |
205 |
mpz_out_str(stdout, 10, alg_b); |
mpz_out_str(stdout, 10, alg_b); |
206 |
printf("\n"); |
printf("\n"); |
207 |
mpz_out_str(stdout, 10, alg_adivb); |
mpz_out_str(stdout, 10, alg_adivb); |
208 |
printf("\n"); |
printf("\n"); |
209 |
mpz_out_str(stdout, 10, alg_amodb); |
mpz_out_str(stdout, 10, alg_amodb); |
210 |
printf("\n"); |
printf("\n"); |
211 |
} |
} |
212 |
while((round < 2000) && (mpz_sgn(alg_amodb) > 0)); |
while((round < 2000) && (mpz_sgn(alg_amodb) > 0)); |
213 |
|
|
214 |
//The GCD canonically will be the last non-zero remainder. |
//The GCD canonically will be the last non-zero remainder. |
215 |
mpz_out_str(stdout, 10, alg_b); |
mpz_out_str(stdout, 10, alg_b); |
216 |
printf("\n"); |
printf("\n"); |
217 |
|
|
218 |
//Finally, we output the trailing "S". |
//Finally, we output the trailing "S". |
219 |
printf("S\n"); |
printf("S\n"); |
220 |
|
|
221 |
return(0); |
return(0); |
222 |
} |
} |
223 |
|
|
224 |
//******************************************************************************** |
//******************************************************************************** |
225 |
// End of SUBFUNC_GCD.C. |
// End of SUBFUNC_GCD.C. |