-
Notifications
You must be signed in to change notification settings - Fork 2
/
zad2_ms.cpp
140 lines (128 loc) · 2.39 KB
/
zad2_ms.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
#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void push_vector(vector<T> &vec, const vector<T> &values)
{
for (int c : values)
{
vec.push_back(c);
}
}
// generate array 0 ... n-1
// if i is prime, then results[i] is true. Otherwise results[i] = false
vector<bool> generate_prime_array(int n)
{
vector<bool> isPrime(n,true);
isPrime[0] = false;
isPrime[1] = false;
isPrime[2] = true;
for (int i = 2; i * i <= n; i++)
{
if (!isPrime[i])
continue;
int j = i * i;
while (j < n)
{
isPrime[j] = false;
j += i;
}
}
return isPrime;
}
// n>=6
// let: Hipoteza Goldbacha zachodzi
vector<int> parity_sum(int n)
{
vector<bool> isPrime = generate_prime_array(n);
int i = 1;
while (true)
{
if (isPrime[i] && isPrime[n - i])
{
vector<int> results{i, n - i};
return results;
}
i++;
}
}
vector<int> rozklad(int n)
{
vector<int> results;
if (n <= 1)
return results;
if (n == 2)
{
results.push_back(2);
return results;
}
if (n == 4)
{
results.push_back(2);
results.push_back(2);
return results;
}
vector<int> parityResults;
if (n % 2 != 0)
{
results.push_back(3);
parityResults = rozklad(n - 3);
}
else
{
parityResults = parity_sum(n);
}
push_vector(results, parityResults);
parityResults.clear();
return results;
}
bool is_prime(int n)
{
if (n <= 1)
return false;
if (n == 2)
return true;
for (int i = 3; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
bool check(int n, const vector<int> r)
{
if (r.size() > 3)
return false;
if (n % 2 == 0)
{
if (r.size() > 2)
return false;
}
int sum = 0;
for (int c : r)
{
if (!is_prime(c))
return false;
sum += c;
}
return sum == n;
}
int main()
{
for (int i = 2; i < 100; i++)
{
vector<int> r = rozklad(i);
cout << i;
if (!check(i, r))
{
cout << " wrong: ";
}
else
cout << " good: ";
for (int c : r)
{
cout << c << " ";
}
cout << endl;
}
}