congruence_solver 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: congruence_solver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - lane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-05 00:00:00.000000000 Z
11
+ date: 2017-02-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '2.4'
47
+ version: '3.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '2.4'
54
+ version: '3.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake-compiler
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -80,14 +80,14 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0.9'
83
- description: "Provides a class (CongruenceSolver) for finding the modular zeros of
84
- a \n polynomial (given the coefficients and modulus) and
85
- a binary (csolve) to \n to solve your congruences at the
86
- command line."
83
+ description: |-
84
+ Provides a class (CongruenceSolver) for finding the modular zeros of a
85
+ polynomial (given the coefficients and modulus) and a binary (csolve) to
86
+ to solve your congruences at the command line.
87
87
  email:
88
88
  - lane.barlow@gmail.com
89
89
  executables:
90
- - csolve.rb
90
+ - csolve
91
91
  - setup
92
92
  extensions:
93
93
  - ext/congruence_solver/extconf.rb
@@ -102,10 +102,9 @@ files:
102
102
  - Rakefile
103
103
  - bench/bench_tools.rb
104
104
  - bench/solve_congruence_bm.rb
105
- - bin/csolve.rb
105
+ - bin/csolve
106
106
  - bin/setup
107
107
  - congruence_solver.gemspec
108
- - ext/congruence_solver/.gitignore
109
108
  - ext/congruence_solver/Makefile
110
109
  - ext/congruence_solver/arith_utils.c
111
110
  - ext/congruence_solver/arith_utils.h
@@ -115,20 +114,16 @@ files:
115
114
  - ext/congruence_solver/extconf.rb
116
115
  - ext/congruence_solver/prime_gen.c
117
116
  - ext/congruence_solver/prime_gen.h
118
- - ext/congruence_solver/test/arith_utils_test.c
119
- - ext/congruence_solver/test/arith_utils_test.h
120
- - ext/congruence_solver/test/congruences_test.c
121
- - ext/congruence_solver/test/congruences_test.h
122
- - ext/congruence_solver/test/prime_gen_test.c
123
- - ext/congruence_solver/test/prime_gen_test.h
124
117
  - lib/congruence_solver.rb
125
118
  - lib/congruence_solver/version.rb
126
119
  - lib/polynomial_interpreter.rb
120
+ - license.txt
127
121
  - spec/congruence_solver_spec.rb
128
122
  - spec/csolve_spec.rb
129
123
  - spec/spec_helper.rb
130
124
  homepage: https://github.com/laneb/congruence_solver
131
- licenses: []
125
+ licenses:
126
+ - Apache-2.0
132
127
  metadata: {}
133
128
  post_install_message:
134
129
  rdoc_options: []
@@ -146,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
141
  version: '0'
147
142
  requirements: []
148
143
  rubyforge_project:
149
- rubygems_version: 2.6.7
144
+ rubygems_version: 2.6.10
150
145
  signing_key:
151
146
  specification_version: 4
152
147
  summary: A gem for solving polynomial congruences.
@@ -1,2 +0,0 @@
1
- todo
2
- *.exe
@@ -1,106 +0,0 @@
1
- #include <stdio.h>
2
- #include "../arith_utils.h"
3
- #include "arith_utils_test.h"
4
-
5
- int main(){
6
- int failures = 0;
7
- int i;
8
-
9
- for(i = 0; i < NUM_OF_MOD_INV_TESTS; i++){
10
- failures += mod_inv_test(MOD_INV_NUMS[i], MOD_INV_MODS[i], MOD_INV_INVS[i]);
11
- }
12
-
13
- for(i = 0; i < NUM_OF_MOD_PRODUCT_TESTS; i++){
14
- failures += mod_product_test(MOD_PRODUCT_NUM_PAIRS[i], MOD_PRODUCT_MODS[i], MOD_PRODUCT_PRODUCTS[i]);
15
- }
16
-
17
- for(i = 0; i < NUM_OF_MOD_POWER_TESTS; i++){
18
- failures += mod_power_test(MOD_POWER_NUMS[i], MOD_POWER_MODS[i], MOD_POWER_POWERS[i], MOD_POWER_EVALS[i]);
19
- }
20
-
21
- for(i = 0; i < NUM_OF_COPRIME_TESTS; i++){
22
- failures += coprime_test(COPRIME_NUM_PAIRS[i], COPRIME_EVALS[i]);
23
- }
24
-
25
- for(i = 0; i < NUM_OF_TOTIENT_TESTS; i++){
26
- failures += totient_test(TOTIENT_NUMS[i], TOTIENT_EVALS[i]);
27
- }
28
-
29
- printf("%d functions failing in arith_utils.c\n\n", failures);
30
- return failures;
31
- }
32
-
33
-
34
- int totient_test(int num, int tot){
35
- int eval_totient = totient(num);
36
-
37
- if(tot != eval_totient){
38
- printf("Totient of %d incorrectly evaluated: %d given instead of %d.\n",
39
- num, eval_totient, tot);
40
- return 1;
41
- }
42
-
43
- else{
44
- return 0;
45
- }
46
- }
47
-
48
-
49
- int coprime_test(int * pair, int isCoprime){
50
- int evalCoprime = coprime(pair[0], pair[1]);
51
-
52
- if(isCoprime && !evalCoprime){
53
- printf("%d and %d incorrectly evaluated as not coprime.\n",
54
- pair[0], pair[1]);
55
- return 1;
56
- }
57
-
58
- else if(!isCoprime && evalCoprime){
59
- printf("%d and %d incorrectly evaluated as coprime.\n",
60
- pair[0], pair[1]);
61
- return 1;
62
- }
63
-
64
- else{
65
- return 0;
66
- }
67
- }
68
-
69
-
70
- int mod_power_test(int num, int mod, int pwr, int mdpwr){
71
- int mdpwrEval = mod_power(num, pwr, mod);
72
-
73
- if(mdpwrEval != mdpwr){
74
- printf("Incorrect evaluation of %d^%d mod %d: %d instead of %d.\n",
75
- num, pwr, mod, mdpwrEval, mdpwr);
76
- return 1;
77
- }
78
-
79
- return 0;
80
- }
81
-
82
-
83
- int mod_product_test(int * pair, int mod, int product){
84
- int prod = mod_product(pair[0], pair[1], mod);
85
-
86
- if(prod != product){
87
- printf("Incorrect evaluation of %d*%d mod %d: %d instead of %d.\n",
88
- pair[0], pair[1], mod, prod, product);
89
- return 1;
90
- }
91
-
92
- return 0;
93
- }
94
-
95
-
96
- int mod_inv_test(int num, int mod, int inv){
97
- int invEval = mod_inv(num, mod);
98
-
99
- if(inv != invEval){
100
- printf("Incorrect evaluation of %d^-1 mod %d: %d instead of %d.\n", num, mod, invEval, inv);
101
- return 1;
102
- }
103
-
104
- return 0;
105
- }
106
-
@@ -1,25 +0,0 @@
1
- #define NUM_OF_MOD_INV_TESTS 4
2
- #define NUM_OF_MOD_PRODUCT_TESTS 3
3
- #define NUM_OF_MOD_POWER_TESTS 4
4
- #define NUM_OF_COPRIME_TESTS 5
5
- #define NUM_OF_TOTIENT_TESTS 6
6
- #define NUM_OF_MOD_EVAL_POLYNOMIAL_TESTS 1
7
-
8
- int TOTIENT_NUMS[NUM_OF_TOTIENT_TESTS] = {1, 2, 3, 41, 125, 9400};
9
- int TOTIENT_EVALS[NUM_OF_TOTIENT_TESTS] = {1, 1, 2, 40, 100, 3680};
10
-
11
- int COPRIME_NUM_PAIRS[NUM_OF_COPRIME_TESTS][2] = {{3,5}, {9, 28}, {100, 34}, {1000512415, 557825}, {2286144, 1515839}};
12
- int COPRIME_EVALS[NUM_OF_COPRIME_TESTS] = {1, 1, 0, 0, 1};
13
-
14
- int MOD_PRODUCT_NUM_PAIRS[NUM_OF_MOD_PRODUCT_TESTS][2] = {{5,6}, {41,3}, {16, 3}};
15
- int MOD_PRODUCT_MODS[NUM_OF_MOD_PRODUCT_TESTS] = {10, 8, 19};
16
- int MOD_PRODUCT_PRODUCTS[NUM_OF_MOD_PRODUCT_TESTS] = {0, 3, 10};
17
-
18
- int MOD_INV_NUMS[NUM_OF_MOD_INV_TESTS] = {5, 4, 53, 3};
19
- int MOD_INV_MODS[NUM_OF_MOD_INV_TESTS] = {12, 23, 105, 7};
20
- int MOD_INV_INVS[NUM_OF_MOD_INV_TESTS] = {5, 6, 2, 5};
21
-
22
- int MOD_POWER_NUMS[NUM_OF_MOD_POWER_TESTS] = {3, 19, 6, 7};
23
- int MOD_POWER_MODS[NUM_OF_MOD_POWER_TESTS] = {4, 23, 7, 33};
24
- int MOD_POWER_POWERS[NUM_OF_MOD_POWER_TESTS] = {10, 3, 4, 635};
25
- int MOD_POWER_EVALS[NUM_OF_MOD_POWER_TESTS] ={1, 5, 1, 10};
@@ -1,78 +0,0 @@
1
- #include <stdio.h>
2
- #include "../congruences.h"
3
- #include "congruences_test.h"
4
-
5
- void print_polynomial_inline(int func_degree, int * func_coeffs);
6
- int solve_congruence_test(int func_degree, int * func_coeffs, int mod, int num_of_solutions, int * solutions);
7
-
8
- int main(){
9
- int failures = 0;
10
-
11
- failures += solve_congruence_test(POL_1_DEGREE, POL_1_COEFFS, POL_1_MOD, NUM_OF_POL_1_SOLS, POL_1_SOLS);
12
- failures += solve_congruence_test(POL_2_DEGREE, POL_2_COEFFS, POL_2_MOD, NUM_OF_POL_2_SOLS, POL_2_SOLS);
13
- failures += solve_congruence_test(POL_3_DEGREE, POL_3_COEFFS, POL_3_MOD, NUM_OF_POL_3_SOLS, POL_3_SOLS);
14
- failures += solve_congruence_test(POL_4_DEGREE, POL_4_COEFFS, POL_4_MOD, NUM_OF_POL_4_SOLS, POL_4_SOLS);
15
- failures += solve_congruence_test(POL_5_DEGREE, POL_5_COEFFS, POL_5_MOD, NUM_OF_POL_5_SOLS, POL_5_SOLS);
16
- failures += solve_congruence_test(POL_6_DEGREE, POL_6_COEFFS, POL_6_MOD, NUM_OF_POL_6_SOLS, POL_6_SOLS);
17
-
18
- return failures;
19
- }
20
-
21
- int int_array_cmp_func(const void * a, const void * b){
22
- return *((int *)a) - *((int *) b);
23
- }
24
-
25
- int solve_congruence_test(int func_degree, int * func_coeffs, int mod, int num_of_solutions, int * solutions){
26
- int * solutions_to_test = solve_congruence(func_degree, func_coeffs, mod);
27
- int i, j;
28
-
29
- qsort(solutions_to_test+1, solutions_to_test[0], sizeof(int), int_array_cmp_func);
30
- qsort(solutions, num_of_solutions, sizeof(int), int_array_cmp_func);
31
-
32
- if(num_of_solutions != solutions_to_test[0]){
33
- printf("Incorrect number of solutions found for congruence ");
34
- print_polynomial_inline(func_degree, func_coeffs);
35
- printf(" = 0 (mod %d): %d given instead of %d.\n\n", mod, solutions_to_test[0], num_of_solutions);
36
-
37
- printf("The following solutions were found: \n");
38
- for(i = 0; i < solutions_to_test[0]; i++){
39
- printf("(%d) %d\n", i, solutions_to_test[i+1]);
40
- }
41
-
42
- printf("\nwhere the actual solutions are\n\n");
43
-
44
- for(i = 0; i < num_of_solutions; i++){
45
- printf("(%d) %d\n", i, solutions[i]);
46
- }
47
-
48
- return 1;
49
- }
50
-
51
- for(i = 0; i < num_of_solutions; i++){
52
- if(solutions[i] != solutions_to_test[i+1]){
53
- printf("Incorrect %dth solution (after sorting) to congruence ", i);
54
-
55
- print_polynomial_inline(func_degree, func_coeffs);
56
-
57
- printf(" = 0 (mod %d): %d given instead of %d.\n\n", mod, solutions_to_test[i+1], solutions[i]);
58
-
59
- return 1;
60
- }
61
- }
62
-
63
- printf("Correct number of solutions (%d) found for congruence ", num_of_solutions);
64
- print_polynomial_inline(func_degree, func_coeffs);
65
- printf(" = 0 (mod %d) without error.\n\n", mod);
66
-
67
- return 0;
68
- }
69
-
70
- void print_polynomial_inline(int func_degree, int * func_coeffs){
71
- int j;
72
-
73
- for(j = func_degree; j >= 1; j--){
74
- printf("%d*x^%d + ", func_coeffs[j], j);
75
- }
76
-
77
- printf("%d", func_coeffs[0]);
78
- }
@@ -1,239 +0,0 @@
1
- #define POL_1_DEGREE 4
2
- #define POL_1_COEFF_0 0
3
- #define POL_1_COEFF_1 9
4
- #define POL_1_COEFF_2 0
5
- #define POL_1_COEFF_3 2
6
- #define POL_1_COEFF_4 1
7
- #define POL_1_MOD 99
8
- #define NUM_OF_POL_1_SOLS 12
9
- #define POL_1_SOL_0 0
10
- #define POL_1_SOL_1 6
11
- #define POL_1_SOL_2 30
12
- #define POL_1_SOL_3 33
13
- #define POL_1_SOL_4 39
14
- #define POL_1_SOL_5 52
15
- #define POL_1_SOL_6 61
16
- #define POL_1_SOL_7 63
17
- #define POL_1_SOL_8 66
18
- #define POL_1_SOL_9 72
19
- #define POL_1_SOL_10 88
20
- #define POL_1_SOL_11 96
21
-
22
-
23
- #define POL_2_DEGREE 6
24
- #define POL_2_COEFF_0 -1
25
- #define POL_2_COEFF_1 0
26
- #define POL_2_COEFF_2 0
27
- #define POL_2_COEFF_3 0
28
- #define POL_2_COEFF_4 0
29
- #define POL_2_COEFF_5 0
30
- #define POL_2_COEFF_6 1
31
- #define POL_2_MOD 700
32
- #define NUM_OF_POL_2_SOLS 24
33
- #define POL_2_SOL_0 1
34
- #define POL_2_SOL_1 51
35
- #define POL_2_SOL_2 99
36
- #define POL_2_SOL_3 101
37
- #define POL_2_SOL_4 149
38
- #define POL_2_SOL_5 151
39
- #define POL_2_SOL_6 199
40
- #define POL_2_SOL_7 201
41
- #define POL_2_SOL_8 249
42
- #define POL_2_SOL_9 251
43
- #define POL_2_SOL_10 299
44
- #define POL_2_SOL_11 349
45
- #define POL_2_SOL_12 351
46
- #define POL_2_SOL_13 401
47
- #define POL_2_SOL_14 449
48
- #define POL_2_SOL_15 451
49
- #define POL_2_SOL_16 499
50
- #define POL_2_SOL_17 501
51
- #define POL_2_SOL_18 549
52
- #define POL_2_SOL_19 551
53
- #define POL_2_SOL_20 599
54
- #define POL_2_SOL_21 601
55
- #define POL_2_SOL_22 649
56
- #define POL_2_SOL_23 699
57
-
58
- #define POL_3_DEGREE 9
59
- #define POL_3_COEFF_0 -11
60
- #define POL_3_COEFF_1 0
61
- #define POL_3_COEFF_2 0
62
- #define POL_3_COEFF_3 3
63
- #define POL_3_COEFF_4 0
64
- #define POL_3_COEFF_5 0
65
- #define POL_3_COEFF_6 0
66
- #define POL_3_COEFF_7 0
67
- #define POL_3_COEFF_8 0
68
- #define POL_3_COEFF_9 10
69
- #define POL_3_MOD 49
70
- #define NUM_OF_POL_3_SOLS 0
71
-
72
- #define POL_4_DEGREE 2
73
- #define POL_4_COEFF_0 4
74
- #define POL_4_COEFF_1 -4
75
- #define POL_4_COEFF_2 1
76
- #define POL_4_MOD 5104
77
- #define NUM_OF_POL_4_SOLS 4
78
- #define POL_4_SOL_0 2
79
- #define POL_4_SOL_1 1278
80
- #define POL_4_SOL_2 2554
81
- #define POL_4_SOL_3 3830
82
-
83
- #define POL_5_DEGREE 11
84
- #define POL_5_COEFF_0 0
85
- #define POL_5_COEFF_1 2
86
- #define POL_5_COEFF_2 0
87
- #define POL_5_COEFF_3 0
88
- #define POL_5_COEFF_4 0
89
- #define POL_5_COEFF_5 0
90
- #define POL_5_COEFF_6 0
91
- #define POL_5_COEFF_7 0
92
- #define POL_5_COEFF_8 0
93
- #define POL_5_COEFF_9 0
94
- #define POL_5_COEFF_10 0
95
- #define POL_5_COEFF_11 1
96
- #define POL_5_MOD 401249
97
- #define NUM_OF_POL_5_SOLS 9
98
- #define POL_5_SOL_0 0
99
- #define POL_5_SOL_1 87850
100
- #define POL_5_SOL_2 101665
101
- #define POL_5_SOL_3 105867
102
- #define POL_5_SOL_4 193717
103
- #define POL_5_SOL_5 207532
104
- #define POL_5_SOL_6 295382
105
- #define POL_5_SOL_7 299584
106
- #define POL_5_SOL_8 313399
107
-
108
- #define POL_6_DEGREE 5
109
- #define POL_6_COEFF_0 1
110
- #define POL_6_COEFF_1 2
111
- #define POL_6_COEFF_2 1
112
- #define POL_6_COEFF_3 -1
113
- #define POL_6_COEFF_4 0
114
- #define POL_6_COEFF_5 -3
115
- #define POL_6_MOD 49
116
- #define NUM_OF_POL_6_SOLS 8
117
- #define POL_6_SOL_0 1
118
- #define POL_6_SOL_1 8
119
- #define POL_6_SOL_2 15
120
- #define POL_6_SOL_3 22
121
- #define POL_6_SOL_4 26
122
- #define POL_6_SOL_5 29
123
- #define POL_6_SOL_6 36
124
- #define POL_6_SOL_7 43
125
-
126
-
127
- int POL_1_COEFFS[POL_1_DEGREE+1] = {POL_1_COEFF_0,
128
- POL_1_COEFF_1,
129
- POL_1_COEFF_2,
130
- POL_1_COEFF_3,
131
- POL_1_COEFF_4};
132
- int POL_1_SOLS[NUM_OF_POL_1_SOLS] = {POL_1_SOL_0,
133
- POL_1_SOL_1,
134
- POL_1_SOL_2,
135
- POL_1_SOL_3,
136
- POL_1_SOL_4,
137
- POL_1_SOL_5,
138
- POL_1_SOL_6,
139
- POL_1_SOL_7,
140
- POL_1_SOL_8,
141
- POL_1_SOL_9,
142
- POL_1_SOL_10,
143
- POL_1_SOL_11};
144
-
145
- int POL_2_COEFFS[POL_2_DEGREE+1] = {POL_2_COEFF_0,
146
- POL_2_COEFF_1,
147
- POL_2_COEFF_2,
148
- POL_2_COEFF_3,
149
- POL_2_COEFF_4,
150
- POL_2_COEFF_5,
151
- POL_2_COEFF_6};
152
-
153
- int POL_2_SOLS[NUM_OF_POL_2_SOLS] = {POL_2_SOL_0,
154
- POL_2_SOL_1,
155
- POL_2_SOL_2,
156
- POL_2_SOL_3,
157
- POL_2_SOL_4,
158
- POL_2_SOL_5,
159
- POL_2_SOL_6,
160
- POL_2_SOL_7,
161
- POL_2_SOL_8,
162
- POL_2_SOL_9,
163
- POL_2_SOL_10,
164
- POL_2_SOL_11,
165
- POL_2_SOL_12,
166
- POL_2_SOL_13,
167
- POL_2_SOL_14,
168
- POL_2_SOL_15,
169
- POL_2_SOL_16,
170
- POL_2_SOL_17,
171
- POL_2_SOL_18,
172
- POL_2_SOL_19,
173
- POL_2_SOL_20,
174
- POL_2_SOL_21,
175
- POL_2_SOL_22,
176
- POL_2_SOL_23};
177
-
178
- int POL_3_COEFFS[POL_3_DEGREE+1] = {POL_3_COEFF_0,
179
- POL_3_COEFF_1,
180
- POL_3_COEFF_2,
181
- POL_3_COEFF_3,
182
- POL_3_COEFF_4,
183
- POL_3_COEFF_5,
184
- POL_3_COEFF_6,
185
- POL_3_COEFF_7,
186
- POL_3_COEFF_8,
187
- POL_3_COEFF_9
188
- };
189
-
190
- int * POL_3_SOLS = NULL;
191
-
192
- int POL_4_COEFFS[POL_4_DEGREE+1] = {POL_4_COEFF_0,
193
- POL_4_COEFF_1,
194
- POL_4_COEFF_2};
195
-
196
- int POL_4_SOLS[NUM_OF_POL_4_SOLS] = {POL_4_SOL_0,
197
- POL_4_SOL_1,
198
- POL_4_SOL_2,
199
- POL_4_SOL_3};
200
-
201
- int POL_5_COEFFS[POL_5_DEGREE+1] = {POL_5_COEFF_0,
202
- POL_5_COEFF_1,
203
- POL_5_COEFF_2,
204
- POL_5_COEFF_3,
205
- POL_5_COEFF_4,
206
- POL_5_COEFF_5,
207
- POL_5_COEFF_6,
208
- POL_5_COEFF_7,
209
- POL_5_COEFF_8,
210
- POL_5_COEFF_9,
211
- POL_5_COEFF_10,
212
- POL_5_COEFF_11};
213
-
214
- int POL_5_SOLS[NUM_OF_POL_5_SOLS] = {POL_5_SOL_0,
215
- POL_5_SOL_1,
216
- POL_5_SOL_2,
217
- POL_5_SOL_3,
218
- POL_5_SOL_4,
219
- POL_5_SOL_5,
220
- POL_5_SOL_6,
221
- POL_5_SOL_7,
222
- POL_5_SOL_8};
223
-
224
- int POL_6_COEFFS[POL_6_DEGREE+1] = {POL_6_COEFF_0,
225
- POL_6_COEFF_1,
226
- POL_6_COEFF_2,
227
- POL_6_COEFF_3,
228
- POL_6_COEFF_4,
229
- POL_6_COEFF_5
230
- };
231
-
232
- int POL_6_SOLS[NUM_OF_POL_6_SOLS] = {POL_6_SOL_0,
233
- POL_6_SOL_1,
234
- POL_6_SOL_2,
235
- POL_6_SOL_3,
236
- POL_6_SOL_4,
237
- POL_6_SOL_5,
238
- POL_6_SOL_6,
239
- POL_6_SOL_7};
@@ -1,83 +0,0 @@
1
- #include <stdio.h>
2
- #include "../prime_gen.h"
3
- #include "prime_gen_test.h"
4
-
5
-
6
-
7
- int main(){
8
- int failures = 0;
9
- int i;
10
- int * prime_ary_to_test = primes_upto(MAX_PRIME_FOR_PRIMES_UPTO_TEST)+1;
11
-
12
- for(i=0; PRIME_ARY[i] <= MAX_PRIME_FOR_PRIMES_UPTO_TEST; i++){
13
- if(PRIME_ARY[i] != prime_ary_to_test[i]){
14
- printf("%dth prime incorrect: %d given instead of %d.\n\n", i, prime_ary_to_test[i], PRIME_ARY[i]);
15
- failures += 1;
16
- }
17
- }
18
-
19
- printf("Primes up to %d generated without error.\n\n", MAX_PRIME_FOR_PRIMES_UPTO_TEST);
20
-
21
- prime_ary_to_test = primes(LIST_LENGTH_FOR_PRIMES_TEST);
22
-
23
- for(i = 0; i < LIST_LENGTH_FOR_PRIMES_TEST; i++){
24
- if(PRIME_ARY[i] != prime_ary_to_test[i]){
25
- printf("%dth prime incorrect: %d given instead of %d.\n\n", i, prime_ary_to_test[i], PRIME_ARY[i]);
26
- failures += 1;
27
- }
28
- }
29
-
30
- printf("First %d primes generated without error.\n\n", LIST_LENGTH_FOR_PRIMES_TEST);
31
-
32
- //Due to the implementation of prime generation and calculation of a maximum divisor
33
- //2 (and 3, for the same reason) arises as a corner case
34
- failures += prime_factors_test(2, FACTORS_OF_2, 1);
35
-
36
- //Factorization of a prime should return an array containig a 1 followed by the prime itself.
37
- failures += prime_factors_test(PRIME_TO_FACTOR, FACTORS_OF_PRIME, 1);
38
-
39
- //Factorization of a composite should return an array containing the number of factors and the
40
- //followed by the factors.
41
- failures += prime_factors_test(SMALL_COMPOSITE, FACTORS_OF_SMALL_COMPOSITE, NUM_OF_SMALL_COMPOSITE_FACTORS);
42
-
43
- //Factorization of composites with factors to powers greater than 1 should still only list each factor once.
44
- failures += prime_factors_test(COMPOSITE_WITH_REPEATED_FACTORS, REPEATED_FACTORS, NUM_OF_REPEATED_FACTORS);
45
-
46
- //Original implementation ran took several minutes to generate the primes necessary
47
- //to factor large numbers.
48
- printf("Beginning to factor a large composite number (%d). A stall here would indicate slow execution.\n",
49
- LARGE_COMPOSITE);
50
- failures += prime_factors_test(LARGE_COMPOSITE, FACTORS_OF_LARGE_COMPOSITE, NUM_OF_LARGE_COMPOSITE_FACTORS);
51
-
52
- //Optimized implementation leverages the least prime factor (once discovered) to minimize the amount of prime generation necessary
53
- //This method will still falter somewhat when the smallest prime factors are large.
54
- printf("Beginning to factor a composite with no small prime factors (%d). A stall here would indicate slow execution.\n",
55
- COMPOSITE_WITH_LARGE_FACTORS);
56
- failures += prime_factors_test(COMPOSITE_WITH_LARGE_FACTORS, LARGE_FACTORS, NUM_OF_LARGE_FACTORS);
57
-
58
- return failures;
59
- }
60
-
61
-
62
- int prime_factors_test(int num, int * expected_factors, int num_of_expected_factors){
63
- int * factor_list = prime_factors(num);
64
- int i;
65
-
66
- if(factor_list[0] != num_of_expected_factors){
67
- printf("Incorrect factorization of prime %d: %d factors given instead of %d.\n\n", num, factor_list[0], num_of_expected_factors);
68
- return 1;
69
- }
70
-
71
- else{
72
- for(i = 0; i < num_of_expected_factors; i++){
73
- if(factor_list[i+1] != expected_factors[i]){
74
- printf("Incorrect 0th factor of %d: %d given.\n\b", num, factor_list[1]);
75
- return 1;
76
- }
77
- }
78
- }
79
-
80
- printf("%d factored correctly.\n\n", num);
81
-
82
- return 0;
83
- }