-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleApplication1.cpp
More file actions
216 lines (192 loc) · 6.05 KB
/
ConsoleApplication1.cpp
File metadata and controls
216 lines (192 loc) · 6.05 KB
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
/*===================================================================================
* Assignment: Program #0 -- programming project for Gdańsk Technical University
*
* Author: Dawid Węsierski
* Language: C++
* To Compile: Microsoft Visual Studio Community 2022 (64-bit) Version 17.2
* Version: 0.1.8
* Date: 05.04.2022
*
* ----------------------------------------------------------------------------------
* Description:
* This project purpose is to practise the use of recursion. This program outputs
* every combination of prime numbers that after adding up will give the number of
* imputed number. You also gives a paramter that will be the biggest prime number
* that will be ouputed. Number our program will be working on shouldnt exceed 256
*
* n=n1+n2+...+nr => n1≥n2≥...≥nr
*
* Imput:
* 0 - int number the number of cases of the problem (called a later)
* 1 - int number (later called n variable) thats the number we will be "spliting"ddddd
* into the prime number
* 2 - int number (later called k variable) thats the number that will be the biggestasd
* prime number that will be in the output ( ONE IS NOT A PRIME NUMBER)
*
* __________________________________________________________________________________
* Example
* Input-------
* 2 #a
* 12 5 #n k
* 27 17 #n k
*
* Output-----
* 5+3+2+2
* 5+5+2
* 17+2+2+2+2+2
* 17+3+3+2+2
* 17+5+3+2
* 17+5+5
* 17+7+3
*
*==================================================================================*/
#include "Header.h"
// this function returns a index of A variable in prime_array ... works like find basicly
// don't judge me
// in future meaby i will make it efficient but works for now
int index_of_prime(int a) {
for (int i = 0; i < prime_array_size; i++) {
if (a == prime_array[i])
return i;
else if (a < prime_array[i]) {
//throw(std::string("your data is not a prime number or is too big"));
return -1;
}
}
return -1;
}
/*
// this function returns a index of A variable in prime_array
// binary search
int index_of_prime(int a) {
}
*/
// this function returns the closest bigger index of a number
// you give the int number and you will get int as a result
int closest_index_of_prime(int a) {
for (int i = 0; i < prime_array_size; i++)
{
if (!(a > prime_array[i]))
return i - 1;
}
throw(std::string("your data is esceding the maximum size of prime array"));
}
void printing_arr(int* start, int* finish)
{
if ((finish - start) > biggest_number_of_divisions ||
finish - start < 0)
{
throw(std::string("the pointer in the printing_arr function aren't pointing to the same array"));
}
while (start != finish)
{
std::cout << *start;
start++;
if (start != finish)
std::cout << "+";
}
}
void repetto(int n, int k, int* aux, int* root)
{
if (k == 0 && n % 2 == 0)
{
printing_arr(root, aux);
if(n)
std::cout << "+";
while (n > 0)
{
std::cout << "2";
n -= 2;
if (n)
std::cout << "+";
}
std::cout << std::endl;
}
else if(k>0)
{
if (n - prime_array[k] > 1)
{
*aux = prime_array[k];
n = n - prime_array[k];
for (int i = 0; (i <= k && (n - prime_array[i] >= 0)); i++)
{
repetto(n, i, aux + 1, root);
}
}
else if ( n - prime_array[k] == 0)
{
*aux = prime_array[k];
repetto(n - prime_array[k], 0, aux + 1, root);
}
else if (n != 4 && k != 1 && root-aux>1) //don't ask why XD this dosn't really have any sens but works
{
repetto(n, k - 1, aux, root);
}
}
}
//this is main function it prints every combination of prime number that can be combined into the number
//input: n-> prime number we will divide
// k-> index of the biggest component in prime_array_size array
void printing_parts(int n, int k)
{
//this is a cringe rezolution of the repetitions problem i will check if this works then meaby rezolve the issue in the future
//if I feel like it OFC <3
int* aux = new int[biggest_number_of_divisions];
aux[0] = prime_array[k];
for (int i = 0; i <= k ; i++)
{
if (n - prime_array[k] - prime_array[i] >= 0)
repetto(n - prime_array[k], i, aux + 1, aux);
else
i = k;
}
delete[] aux;
}
int main()
{
/*
std::cout << biggest_number_of_divisions << " <-biggest_number_of_divisions" << std::endl;
std::cout << prime_array_size << " <-prime_array_size" << std::endl;
for (int i = 0; i < 100; i++)
{
std::cout << i << " <-i " << closest_index_of_prime(i) << " <-index " << prime_array[closest_index_of_prime(i)] << std::endl;
}
*/
/* TESTING */
int a, n, k;
try
{
std::cin >> a;
for (int iterator = 0; iterator < a; iterator++) {
try {
for (int i = 0; i < a; i++)
{
std::cin >> n; // number we will be spliting
std::cin >> k; // the biggest part of every way of decomposition our program makes
if (index_of_prime(k) >= 0)
{
if (n == k)
std::cout << n << std::endl;
else
printing_parts(n, index_of_prime(k));
}
}
}
catch (std::string problem)
{
std::cout << "program encoutered an error within component listed below:" << std::endl;
std::cout << problem << std::endl;
}
catch (...)
{
std::cout << "Program encountered an unhandled exception" << std::endl;
std::cout << "try again" << std::endl;
}
}
}
catch (...)
{
std::cout << "unhandled expection";
}
/* TESTING DONE */
}