RTXI  2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abstmath.cpp
Go to the documentation of this file.
1 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 //
3 // File = abstmath.cpp
4 //
5 // Mathematical Operations for Abstract Algebra
6 //
7 
8 #include "abstmath.h"
9 #include "misdefs.h"
10 #include <fstream>
11 #include <iostream>
12 #include <math.h>
13 #include <stdlib.h>
14 
15 //======================================================
16 // constructor
17 
18 PrimeFactorSet::PrimeFactorSet(int num_factors, int* factors)
19 {
20  int n1, n2, match;
21  int distinct_factor_tally;
22  Num_Factors = num_factors;
24  for (n1 = 1; n1 < num_factors; n1++) {
25  match = 0;
26  for (n2 = 0; n2 < n1; n2++) {
27  // compare factor n1 to all other factors that
28  // come before it in the vector factors[].
29  // if no match, increment count of distinct factors
30  if (factors[n1] == factors[n2])
31  match = 1;
32  }
33  if (match == 1)
34  continue;
36  }
39  Factor_Vector[0] = factors[0];
40  Factor_Multiplicity[0] = 1;
41  distinct_factor_tally = 1;
42  for (n1 = 1; n1 < num_factors; n1++) {
43  match = 0;
44  for (n2 = 0; n2 < distinct_factor_tally; n2++) {
45  if (factors[n1] != Factor_Vector[n2])
46  continue;
47  Factor_Multiplicity[n2]++;
48  match = 1;
49  break;
50  }
51  if (match)
52  continue;
53  Factor_Vector[distinct_factor_tally] = factors[n1];
54  Factor_Multiplicity[distinct_factor_tally] = 1;
55  distinct_factor_tally++;
56  }
57  for (n1 = 0; n1 < Num_Distinct_Factors; n1++) {
58  std::cout << Factor_Vector[n1] << " ** " << Factor_Multiplicity[n1]
59  << std::endl;
60  }
61  return;
62 };
63 
65 {
66  delete[] Factor_Vector;
67  delete[] Factor_Multiplicity;
68 }
69 
70 //======================================================
71 // constructor
72 
73 OrderedFactorSet::OrderedFactorSet(int num_factors, int* factors)
74 {
75  int n;
76  Num_Factors = num_factors;
77 
78  Factor_Vector = new int[Num_Factors];
79 
80  for (n = 0; n < num_factors; n++) {
81  Factor_Vector[n] = factors[n];
82  }
83  return;
84 }
85 
87 {
88  delete[] Factor_Vector;
89 }
90 
92 PrimeFactorization(int number)
93 {
94  int num_factors;
95  int first_n, geom_middle;
96  int residue;
97  int factor_not_found;
98  int* temp_factors = new int[20];
99 
100  residue = number;
101 
102  geom_middle = (int)ceil(sqrt(double(residue)));
103  factor_not_found = 1;
104  num_factors = 0;
105  first_n = 2;
106 
107  for (;;) {
108  for (int n = first_n; n <= geom_middle; n++) {
109  if (residue % n != 0)
110  continue;
111  factor_not_found = 0;
112  temp_factors[num_factors] = n;
113  num_factors++;
114  residue /= n;
115  geom_middle = (int)ceil(sqrt(double(residue)));
116  first_n = n;
117  std::cout << "found factor " << n << std::endl;
118  break;
119  }
120  if (residue == 1)
121  break;
122  if (factor_not_found)
123  break;
124  factor_not_found = 1;
125  }
126  if (residue != 1) {
127  temp_factors[num_factors] = residue;
128  num_factors++;
129  }
130  std::cout << "final residue is " << residue << std::endl;
131  PrimeFactorSet* factor_set = new PrimeFactorSet(num_factors, temp_factors);
132 
133  delete[] temp_factors;
134  return factor_set;
135 }
int * Factor_Vector
Definition: abstmath.h:30
~PrimeFactorSet(void)
Definition: abstmath.cpp:64
int Num_Factors
Definition: abstmath.h:15
PrimeFactorSet * PrimeFactorization(int number)
Definition: abstmath.cpp:92
PrimeFactorSet(int num_factors, int *factors)
Definition: abstmath.cpp:18
int * Factor_Multiplicity
Definition: abstmath.h:18
complex sqrt(const complex _z)
Definition: complex.h:148
int * Factor_Vector
Definition: abstmath.h:17
~OrderedFactorSet(void)
Definition: abstmath.cpp:86
OrderedFactorSet(int num_factors, int *factors)
Definition: abstmath.cpp:73
int Num_Distinct_Factors
Definition: abstmath.h:16