1 |
//$Header: /cvsroot/esrg/sfesrg/esrgnxpj/sfnthcgi0304/arith_large_cgi.c,v 1.6 2003/07/01 03:46:58 dtashley Exp $ |
//$Header$ |
2 |
//******************************************************************************** |
//******************************************************************************** |
3 |
//This source file is the main function of a program to support number theory |
//Copyright (c) 2003, 2018 David T. Ashley. |
4 |
//web pages. |
//******************************************************************************** |
5 |
//******************************************************************************** |
//This file is part of "arith_large_cgi", a program that is designed to be |
6 |
//Copyright (C) 2003 David T. Ashley |
//invoked by a PHP script as part of serving a web page that performs |
7 |
//******************************************************************************** |
//calculations involving large integers. (A secondary compiled program is |
8 |
//This program is free software; you can redistribute it and/or modify |
//used because a compiled program can perform certain calculation-intensive |
9 |
//it under the terms of the GNU General Public License as published by |
//tasks far more efficiently than a PHP script.) This program is provided by |
10 |
//the Free Software Foundation; either version 2 of the License, or |
//David T. Ashley (dashley@gmail.com) under the MIT License (reproduced |
11 |
//(at your option) any later version. |
//immediately below). |
12 |
// |
//******************************************************************************** |
13 |
//This program or source file is distributed in the hope that it will |
//Permission is hereby granted, free of charge, to any person obtaining a copy |
14 |
//be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of |
//of this software and associated documentation files (the "Software"), to deal |
15 |
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
//in the Software without restriction, including without limitation the rights |
16 |
//GNU General Public License for more details. |
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
17 |
// |
//copies of the Software, and to permit persons to whom the Software is |
18 |
//You may have received a copy of the GNU General Public License |
//furnished to do so, subject to the following conditions: |
19 |
//along with this program; if not, write to the Free Software |
// |
20 |
//Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
//The above copyright notice and this permission notice shall be included in all |
21 |
//******************************************************************************** |
//copies or substantial portions of the Software. |
22 |
//This program is written exclusively to support a PHP script which must calculate and |
// |
23 |
//display a number of number theory results. The necessity of this script |
//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
24 |
//(rather than using BCMATH) comes about because SourceForge does not have |
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
25 |
//bcmath installed on its PHP system. |
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
26 |
// |
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
27 |
//This program is meant to be called by the PHP script using the exec() |
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
28 |
//call, but could also be run by hand (and in fact this method is used for testing). |
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
29 |
// |
//SOFTWARE. |
30 |
//The first parameter after the program name is the suboperation to be performed |
//******************************************************************************** |
31 |
//(each CGI page probably uses a different suboperation). This program |
//This program is written exclusively to support a PHP script which must calculate and |
32 |
//will return a non-zero exit code only if there is a missing or unrecognized |
//display a number of number theory results. The necessity of this script |
33 |
//suboperation. Otherwise, error reporting is controlled by the rules of the |
//(rather than using BCMATH) comes about because SourceForge does not have |
34 |
//suboperation. |
//bcmath installed on its PHP system. |
35 |
// |
// |
36 |
//The currently defined suboperations are: |
//This program is meant to be called by the PHP script using the exec() |
37 |
// |
//call, but could also be run by hand (and in fact this method is used for testing). |
38 |
// a)"help" |
// |
39 |
// Display information about what the program is and how to use |
//The first parameter after the program name is the suboperation to be performed |
40 |
// it. |
//(each CGI page probably uses a different suboperation). This program |
41 |
// b)"gcd" |
//will return a non-zero exit code only if there is a missing or unrecognized |
42 |
// Calculate the gcd of two integers using Euclid's classic |
//suboperation. Otherwise, error reporting is controlled by the rules of the |
43 |
// algorithm and also display the intermediate results. |
//suboperation. |
44 |
// c)"gmp_prob_prime" |
// |
45 |
// Use Miller-Rabin to determine to high certainty whther a |
//The currently defined suboperations are: |
46 |
// number is prime or composite. |
// |
47 |
// d)"pfact_18" |
// a)"help" |
48 |
// Attempt to factor a number of 18 decimal digits or less into |
// Display information about what the program is and how to use |
49 |
// component primes. |
// it. |
50 |
// e)"cfbrap" |
// b)"gcd" |
51 |
// Finds the best rational approximation to a rational number subject |
// Calculate the gcd of two integers using Euclid's classic |
52 |
// to constraints on the denominator and numerator using continued |
// algorithm and also display the intermediate results. |
53 |
// fraction techniques. |
// c)"gmp_prob_prime" |
54 |
|
// Use Miller-Rabin to determine to high certainty whther a |
55 |
#include <stdio.h> |
// number is prime or composite. |
56 |
#include <stdlib.h> |
// d)"pfact_18" |
57 |
#include <string.h> |
// Attempt to factor a number of 18 decimal digits or less into |
58 |
|
// component primes. |
59 |
#include "auxfuncs.h" |
// e)"cfbrap" |
60 |
#include "subfunc_cfbrap.h" |
// Finds the best rational approximation to a rational number subject |
61 |
#include "subfunc_gcd.h" |
// to constraints on the denominator and numerator using continued |
62 |
#include "subfunc_gmp_prob_prime.h" |
// fraction techniques. |
63 |
#include "subfunc_pfact_18.h" |
|
64 |
|
#include <stdio.h> |
65 |
//Single function prototype needed for a forward reference. |
#include <stdlib.h> |
66 |
static void dump_subfunction_choices(int indent); |
#include <string.h> |
67 |
|
|
68 |
//Implements the help. |
#include "auxfuncs.h" |
69 |
int SUBFUNC_HELP_main(int argc, char *argv[]) |
#include "subfunc_cfbrap.h" |
70 |
{ |
#include "subfunc_gcd.h" |
71 |
int i; |
#include "subfunc_gmp_prob_prime.h" |
72 |
char *help[] = |
#include "subfunc_pfact_18.h" |
73 |
{ |
|
74 |
"This program is a compiled 'C'-language program designed to be called from a", |
//Single function prototype needed for a forward reference. |
75 |
"PHP script to assist in calculating number theory and large integer", |
static void dump_subfunction_choices(int indent); |
76 |
"arithmetic results. This scheme (of having a separate executable like this", |
|
77 |
"one) was used with SourceForge web content because SourceForge did not have", |
//Implements the help. |
78 |
"the bcmath library compiled into PHP, and it was determined experimentally", |
int SUBFUNC_HELP_main(int argc, char *argv[]) |
79 |
"that writing large integer arithmetic functions in PHP directly resulted in", |
{ |
80 |
"unusably slow performance. This program makes use of the GMP library, which", |
int i; |
81 |
"gives it superior performance.", |
char *help[] = |
82 |
"", |
{ |
83 |
"This program can of course be invoked from a shell or from another scripting", |
"This program is a compiled 'C'-language program designed to be called from a", |
84 |
"language besides PHP. It is designed, however, for execution from a", |
"PHP script to assist in calculating number theory and large integer", |
85 |
"scripting language, as you might guess from the human-unfriendly output.", |
"arithmetic results. This scheme (of having a separate executable like this", |
86 |
"", |
"one) was used with SourceForge web content because SourceForge did not have", |
87 |
"Please contact Dave Ashley (dtashley@aol.com) with any questions or", |
"the bcmath library compiled into PHP, and it was determined experimentally", |
88 |
"concerns.", |
"that writing large integer arithmetic functions in PHP directly resulted in", |
89 |
"", |
"unusably slow performance. This program makes use of the GMP library, which", |
90 |
"Dave Ashley, Detroit, Michigan, USA, April, 2003.", |
"gives it superior performance.", |
91 |
"", |
"", |
92 |
"The available subfunction choices (the first parameter on the command", |
"This program can of course be invoked from a shell or from another scripting", |
93 |
"line) are:", |
"language besides PHP. It is designed, however, for execution from a", |
94 |
}; |
"scripting language, as you might guess from the human-unfriendly output.", |
95 |
|
"", |
96 |
for (i=0; i<sizeof(help)/sizeof(help[0]); i++) |
"Please contact Dave Ashley (dtashley@aol.com) with any questions or", |
97 |
printf("%s\n", help[i]); |
"concerns.", |
98 |
dump_subfunction_choices(3); |
"", |
99 |
} |
"Dave Ashley, Detroit, Michigan, USA, April, 2003.", |
100 |
|
"", |
101 |
//Structure type used to hold the jump table of different functions |
"The available subfunction choices (the first parameter on the command", |
102 |
//to handle different subcommands. |
"line) are:", |
103 |
struct struct_ARITH_LARGE_CGI_subcmd_jmp |
}; |
104 |
{ |
|
105 |
char *subfunc_string; |
for (i=0; i<sizeof(help)/sizeof(help[0]); i++) |
106 |
int (*subfunc_ptr)(int argc, char *argv[]); |
printf("%s\n", help[i]); |
107 |
}; |
dump_subfunction_choices(3); |
108 |
|
} |
109 |
//The jump table. The last element is defined to have both a |
|
110 |
//NULL string pointer and a NULL function pointer. |
//Structure type used to hold the jump table of different functions |
111 |
static struct struct_ARITH_LARGE_CGI_subcmd_jmp subcmd_jump_tbl[] = |
//to handle different subcommands. |
112 |
{ |
struct struct_ARITH_LARGE_CGI_subcmd_jmp |
113 |
{"cfbrap", SUBFUNC_CFBRAP_main }, |
{ |
114 |
{"gcd", SUBFUNC_GCD_main }, |
char *subfunc_string; |
115 |
{"gmp_prob_prime", SUBFUNC_GMP_PROB_PRIME_main }, |
int (*subfunc_ptr)(int argc, char *argv[]); |
116 |
{"pfact_18", SUBFUNC_PFACT_18_main }, |
}; |
117 |
{"help", SUBFUNC_HELP_main }, |
|
118 |
{NULL, NULL } |
//The jump table. The last element is defined to have both a |
119 |
}; |
//NULL string pointer and a NULL function pointer. |
120 |
|
static struct struct_ARITH_LARGE_CGI_subcmd_jmp subcmd_jump_tbl[] = |
121 |
//Dumps the available choices for the subfunction code to the |
{ |
122 |
//standard output, each indented by "indent" characters. |
{"cfbrap", SUBFUNC_CFBRAP_main }, |
123 |
static void dump_subfunction_choices(int indent) |
{"gcd", SUBFUNC_GCD_main }, |
124 |
{ |
{"gmp_prob_prime", SUBFUNC_GMP_PROB_PRIME_main }, |
125 |
int i, j; |
{"pfact_18", SUBFUNC_PFACT_18_main }, |
126 |
|
{"help", SUBFUNC_HELP_main }, |
127 |
for (i=0; subcmd_jump_tbl[i].subfunc_string != NULL; i++) |
{NULL, NULL } |
128 |
{ |
}; |
129 |
for (j=0; j<indent; j++) |
|
130 |
printf(" "); |
//Dumps the available choices for the subfunction code to the |
131 |
printf("%s\n", subcmd_jump_tbl[i].subfunc_string); |
//standard output, each indented by "indent" characters. |
132 |
} |
static void dump_subfunction_choices(int indent) |
133 |
} |
{ |
134 |
|
int i, j; |
135 |
|
|
136 |
int main(int argc, char *argv[]) |
for (i=0; subcmd_jump_tbl[i].subfunc_string != NULL; i++) |
137 |
{ |
{ |
138 |
int i; |
for (j=0; j<indent; j++) |
139 |
int rv; |
printf(" "); |
140 |
|
printf("%s\n", subcmd_jump_tbl[i].subfunc_string); |
141 |
if (argc < 2) |
} |
142 |
{ |
} |
143 |
printf("Missing subfunction code. Choices are:\n"); |
|
144 |
dump_subfunction_choices(3); |
|
145 |
exit(4); |
int main(int argc, char *argv[]) |
146 |
} |
{ |
147 |
|
int i; |
148 |
for (i=0; subcmd_jump_tbl[i].subfunc_string != NULL; i++) |
int rv = 0; |
149 |
{ |
|
150 |
if (!strcmp(argv[1], subcmd_jump_tbl[i].subfunc_string)) |
if (argc < 2) |
151 |
{ |
{ |
152 |
rv = (subcmd_jump_tbl[i].subfunc_ptr)(argc, argv); |
printf("Missing subfunction code. Choices are:\n"); |
153 |
goto normal_return; |
dump_subfunction_choices(3); |
154 |
} |
exit(4); |
155 |
} |
} |
156 |
|
|
157 |
printf("Invalid subfunction code. Choices are:\n"); |
for (i=0; subcmd_jump_tbl[i].subfunc_string != NULL; i++) |
158 |
dump_subfunction_choices(3); |
{ |
159 |
exit(4); |
if (!strcmp(argv[1], subcmd_jump_tbl[i].subfunc_string)) |
160 |
|
{ |
161 |
normal_return: |
rv = (subcmd_jump_tbl[i].subfunc_ptr)(argc, argv); |
162 |
exit(rv); |
goto normal_return; |
163 |
} |
} |
164 |
|
} |
165 |
//******************************************************************************** |
|
166 |
//$Log: arith_large_cgi.c,v $ |
printf("Invalid subfunction code. Choices are:\n"); |
167 |
//Revision 1.6 2003/07/01 03:46:58 dtashley |
dump_subfunction_choices(3); |
168 |
//Edits towards working continued fraction best rational approximation |
exit(4); |
169 |
//functionality. |
|
170 |
// |
normal_return: |
171 |
//Revision 1.5 2003/06/29 22:51:49 dtashley |
exit(rv); |
172 |
//Edits toward CF best approximation functionality. |
} |
173 |
// |
|
174 |
//Revision 1.4 2003/04/17 20:02:05 dtashley |
//******************************************************************************** |
175 |
//License text for the GPL added. All source files are now under the GPL, |
//End of file ARITH_LARGE_CGI.C. |
176 |
//after some discussion on the GMP list. |
//******************************************************************************** |
|
// |
|
|
//Revision 1.3 2003/04/15 23:55:43 dtashley |
|
|
//SUBFUNC_PFACT_18 added. |
|
|
// |
|
|
//Revision 1.2 2003/04/14 23:27:01 dtashley |
|
|
//Subfunction GMP_PROB_PRIME finished. |
|
|
// |
|
|
//Revision 1.1 2003/04/14 20:38:40 dtashley |
|
|
//Initial checkin. |
|
|
//******************************************************************************** |
|
|
//End of file ARITH_LARGE_CGI.C. |
|
|
//******************************************************************************** |
|