-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGoldbachPartitions.cpp
258 lines (217 loc) · 9.4 KB
/
GoldbachPartitions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*************************************************************************************
* Author: Ahmed Iqbal
* Course: CS1336.010
* Date: 11/16/2020
* Assignment: Lecture Homework #9 Exercise #1
* Complier: Visual C++ 2019
*
* Description: This program prints out all of the binary GoldBach partitions for
* even numbers from 4 to 100. it also prints out all of the ternary
* GoldBach partitions for all numbers from 6 to 100
*************************************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// creating prototypes
bool isPrime(int prime);
void nextPrime(int& number);
int main()
{
// ***************************************************************************************************************
// * I combined Ex1 and Bonus 2 into the same code because they both deal with the same conjecture.
// ***************************************************************************************************************
// declaring and initializing variables used for both Conjectures
int x, y, z, count = 0, maxCount = 0, maxPartitions = 0;
// printing header
cout << "*******************************************************************************************" << endl
<< "**********************************Binary GoldBach Partitions*******************************" << endl
<< "*******************************************************************************************" << endl << endl;
// setting for loop to increment by 2 from 4 up until 100
for (int i = 4; i <= 100; i += 2)
{
// re-initializing x and y to 2 each iteration
x = 2, y = 2;
// printing one "Even integer" per iteration
cout << "Even integer " << setw(3) << i;
// printing out partition and incrementing count if partition is met
if (i == (x + y) && x >= y)
{
cout << " = [" << x << " + " << y << "]";
count++;
}
else
{
// setting while loop to run as long as a partition is not met and x is
// less than or equal to 97 (highest prime number less than 100)
while (i != (x + y) && x <= 97)
{
// incrementing x until x is the next prime number
nextPrime(x);
// printing out partition and incrementing count if partition is now met and
// if x is greater than y (to avoid permutations of the same partition)
if (i == (x + y) && (x >= y))
{
cout << " = [" << x << " + " << y << "]";
count++;
}
else
{
// setting while loop to run as long as a partition is not met and y is
// less than or equal to 97 (highest prime number less than 100)
while (i != (x + y) && y <= 97)
{
// incrementing y until y is the next prime number
nextPrime(y);
// printing out partition and incrementing count if partition is now met and
// if x is greater than y (to avoid permutations of the same partition)
if (i == (x + y) && (x >= y))
{
cout << " = [" << x << " + " << y << "]";
count++;
}
}
}
// re-initializing y to 2 for next iteration
y = 2;
}
}
// setting count equal to maxCount and maxPartitions equal to i (iteration) if count is great than maxCount
if (count > maxCount)
{
maxCount = count;
maxPartitions = i;
}
// resetting count to 0 each iteration
count = 0;
cout << endl << endl;
}
// printing out the even integer with the most partitions
cout << "The even integer with the greatest number of binary partitions is " << maxPartitions << endl << endl;
// ***************************************************************************************************************
// * This is the bonus assignment that prints out ternary paritions. I took the "at least" requirement to heart
// * and printed out all possible ternary partitions. Enjoy ;^)
// ***************************************************************************************************************
// re-initializing variable to 0
count = 0, maxCount = 0, maxPartitions = 0;
// printing header
cout << "*******************************************************************************************" << endl
<< "*********************************Ternary GoldBach Partitions*******************************" << endl
<< "*******************************************************************************************" << endl << endl;
// setting for loop to increment by 1 from 6 up until 100
for (int i = 6; i <= 100; i++)
{
// re-initializing x and y to 2 each iteration
x = 2, y = 2, z = 2;
// printing one "Integer" per iteration
cout << "Integer " << setw(3) << i;
// printing out partition and incrementing count if partition is met
if (i == (x + y + z) && x <= 97)
{
cout << " = [" << x << " + " << y << " + " << z << "]";
count++;
}
else
{
// setting while loop to run as long as a partition is not met and x is
// less than or equal to 97 (highest prime number less than 100)
while (i != (x + y + z) && x <= 97)
{
// incrementing x until x is the next prime number
nextPrime(x);
if (i == (x + y + z) && (x >= y) && (y >= z))
{
cout << " = [" << x << " + " << y << " + " << z << "]";
count++;
}
else
{
// setting while loop to run as long as a partition is not met and y is
// less than or equal to 97 (highest prime number less than 100)
while (i != (x + y + z) && y <= 97)
{
// calling nextPrime to get
nextPrime(y);
if (i == (x + y + z) && (x >= y) && (y >= z))
{
cout << " = [" << x << " + " << y << " + " << z << "]";
count++;
}
else
{
// setting while loop to run as long as a partition is not met and z is
// less than or equal to 97 (highest prime number less than 100)
while (i != (x + y + z) && z <= 97)
{
// incrementing z until z is the next prime number
nextPrime(z);
if (i == (x + y + z) && (x >= y) && (y >= z))
{
cout << " = [" << x << " + " << y << " + " << z << "]";
count++;
}
}
}
// re-initializing z to 2 for next iteration
z = 2;
}
}
// re-initializing y to 2 for next iteration
y = 2;
}
}
// setting count equal to maxCount and maxPartitions equal to i (iteration) if count is great than maxCount
if (count > maxCount)
{
maxCount = count;
maxPartitions = i;
}
// resetting count to 0 each iteration
count = 0;
cout << endl << endl;
}
// printing out the integer with the most partitions
cout << "The integer with the greatest number of ternary partitions is " << maxPartitions << endl << endl;
return 0;
} // end function main
/****************************************************************************
* Function: isPrime(int prime)
* Descr: This function takes an int as an input and divides it by every
* number from 2 to int - 1 to check whether it is prime. If the
* number is divisible by any of those numbers, isPrime() returns
* a false value.
* Input: prime
* The integer input.
* Return: a bool value.
****************************************************************************/
bool isPrime(int currNum)
{
// declaring and initializing isPrime to true as default
bool isPrime = true;
// setting for loop to run 2 to "currNum" - 1 times
for (int i = 2; i <= currNum - 1; i++)
{
// if currNum is divisible by i, the function returns false
if (currNum % i == 0)
{
isPrime = false;
break;
}
}
// if isPrime isn't change by now, the number is a prime
return isPrime;
}
/****************************************************************************
* Function: nextPrime(int& number)
* Descr: This function increments x until x is the next prime number
* Input: the reference integer number
* Return: nothing.
****************************************************************************/
void nextPrime(int& number)
{
number++;
// as long as number is not prime, it will be incremented
while (!isPrime(number))
{
number++;
}
}