Cho số nguyên N, nhiệm vụ là tìm ba số nguyên tố X, Y, Z sao cho tổng của ba số này bằng N i. e. X + Y + Z = N
ví dụ.
Đầu vào. N = 20
đầu ra. 2 5 13Đầu vào. N = 34
đầu ra. 2 3 29
Tiếp cận.
- Tạo số nguyên tố bằng Sàng của Eratosthenes
- Bắt đầu từ số nguyên tố đầu tiên
- Lấy một số khác từ danh sách đã tạo
- Trừ số thứ nhất và số thứ hai từ số ban đầu để được số thứ ba
- Kiểm tra xem số thứ ba có phải là số nguyên tố không
- Nếu số thứ ba là số nguyên tố thì xuất ra ba số
- Nếu không, lặp lại quy trình cho số thứ hai và do đó, số đầu tiên
- Nếu câu trả lời không tồn tại thì in -1
Dưới đây là việc thực hiện các phương pháp trên.
C++
// C++ implementation of the approach
#include
using
namespace
std;
const
int
MAX = 100001;
________số 8_______
// C++ implementation of the approach
0
// C++ implementation of the approach
1int
// C++ implementation of the approach
3
// C++ implementation of the approach
4
// C++ implementation of the approach
5 // C++ implementation of the approach
6
// C++ implementation of the approach
7
// C++ implementation of the approach
8_______0_______9
// C++ implementation of the approach
8_______1_______1 #include
2
// C++ implementation of the approach
8_______6_______ #include
5
// C++ implementation of the approach
8_______1_______7 #include
8int
using
0
using
1using
2 using
3
using
4_______1_______7 #include
8int
using
8
using
9namespace
0
using
9namespace
2namespace
3namespace
4
// C++ implementation of the approach
8namespace
6
// C++ implementation of the approach
8namespace
8
// C++ implementation of the approach
8std;
0
// C++ implementation of the approach
8_______1_______7 #include
8int
std;
5
using
1using
2 std;
8std;
9const
0
using
4_______5_______2
const
3
const
4
const
5
// C++ implementation of the approach
5 const
7int
const
9
// C++ implementation of the approach
7
// C++ implementation of the approach
8_______1_______1 int
3_______4_______9_______3_______4
// C++ implementation of the approach
8_______6_______ int
8
// C++ implementation of the approach
8_______1_______7 #include
8int
MAX = 100001;
3
using
1MAX = 100001;
5
using
1MAX = 100001;
7
using
1#include
7 #include
8int
// The vector primes holds
2
using
4_______8_______4
using
4_______8_______6
using
4_______8_______8
using
4_______0_______00
using
4_______0_______02
using
4_______0_______04
using
4_______2_______2 // C++ implementation of the approach
07
// C++ implementation of the approach
08// C++ implementation of the approach
09
using
9int
3_______3_______3namespace
4
using
9// C++ implementation of the approach
15namespace
4
using
4_______5_______3
using
1const
3
using
1using
2 // C++ implementation of the approach
23
using
4_______0_______15namespace
4
// C++ implementation of the approach
8_______5_______3
// C++ implementation of the approach
8_______0_______30
// C++ implementation of the approach
8_______0_______32
// C++ implementation of the approach
8_______2_______2 // C++ implementation of the approach
23
using
1// C++ implementation of the approach
37// C++ implementation of the approach
38
// C++ implementation of the approach
39_______0_______40// C++ implementation of the approach
38 // C++ implementation of the approach
42
// C++ implementation of the approach
8_______0_______44
using
1// C++ implementation of the approach
46
const
3
// C++ implementation of the approach
48
int
// C++ implementation of the approach
50
// C++ implementation of the approach
7
// C++ implementation of the approach
8_______6_______ // C++ implementation of the approach
54
// C++ implementation of the approach
8_______0_______56
// C++ implementation of the approach
8_______0_______58
// C++ implementation of the approach
8_______0_______60
// C++ implementation of the approach
8// C++ implementation of the approach
62
// C++ implementation of the approach
8// C++ implementation of the approach
64
// C++ implementation of the approach
8// C++ implementation of the approach
66
// C++ implementation of the approach
8_______0_______68 // C++ implementation of the approach
69
const
3
Java
// C++ implementation of the approach
71
// C++ implementation of the approach
72 // C++ implementation of the approach
73
// C++ implementation of the approach
72 // C++ implementation of the approach
75
// C++ implementation of the approach
72 // C++ implementation of the approach
77
// C++ implementation of the approach
78 // C++ implementation of the approach
79
// C++ implementation of the approach
80 int
// C++ implementation of the approach
82// C++ implementation of the approach
83namespace
4
// C++ implementation of the approach
85
________số 8_______
// C++ implementation of the approach
0
// C++ implementation of the approach
80 // C++ implementation of the approach
89
// C++ implementation of the approach
85
// C++ implementation of the approach
4
// C++ implementation of the approach
80 // C++ implementation of the approach
5 // C++ implementation of the approach
6
// C++ implementation of the approach
7
// C++ implementation of the approach
8
// C++ implementation of the approach
8_______0_______9
// C++ implementation of the approach
8_______1_______00#include
01_______1_______02 #include
00#include
04#include
05#include
06
// C++ implementation of the approach
8_______6_______ #include
5
// C++ implementation of the approach
8
// C++ implementation of the approach
8_______1_______7#include
8int
#include
15#include
16#include
17
using
1using
2 using
3
using
4_______1_______7#include
8int
using
8
using
4
using
9namespace
0
using
9namespace
2namespace
3namespace
4
// C++ implementation of the approach
85
// C++ implementation of the approach
8namespace
6
// C++ implementation of the approach
8namespace
8
// C++ implementation of the approach
8std;
0
// C++ implementation of the approach
8_______1_______7#include
8int
#include
15#include
16#include
46
using
1using
2 std;
8std;
9const
0
using
4_______1_______53
const
3
// C++ implementation of the approach
85
const
4
const
5
// C++ implementation of the approach
80 // C++ implementation of the approach
5 const
7int
const
9
// C++ implementation of the approach
7
// C++ implementation of the approach
8_______1_______00 int
3_______4_______9namespace
4
// C++ implementation of the approach
8_______6_______ #include
71#include
05#include
73#include
05#include
75#include
05_______3_______4
// C++ implementation of the approach
8
// C++ implementation of the approach
8_______1_______7#include
8int
#include
15#include
84#include
85
// C++ implementation of the approach
8_______0_______7
using
1
using
1MAX = 100001;
5
using
1#include
92
using
1
using
1#include
7#include
8_______6_______ #include
98#include
84using
00
using
1// C++ implementation of the approach
7
using
4
using
4_______8_______4
using
4_______2_______07
// C++ implementation of the approach
85
using
4_______8_______8
using
4_______0_______00
using
4_______0_______02
// C++ implementation of the approach
85
using
4_______0_______04
using
4_______2_______2 using
20
using
9using
22#include
84const
0
using
4_______0_______7
using
9int
3_______3_______3namespace
4
using
9// C++ implementation of the approach
15namespace
4
using
4_______5_______3
using
1const
3
using
1using
2 // C++ implementation of the approach
23
using
4_______0_______15namespace
4
// C++ implementation of the approach
8_______5_______3
// C++ implementation of the approach
8
// C++ implementation of the approach
8_______0_______30
// C++ implementation of the approach
8_______0_______32
// C++ implementation of the approach
8_______2_______2 // C++ implementation of the approach
23
using
54using
55// C++ implementation of the approach
38 using
57
using
58using
59// C++ implementation of the approach
38 using
57
using
58using
63
// C++ implementation of the approach
8_______0_______44
using
1using
67_______1_______05 using
69
const
3
// C++ implementation of the approach
48
using
72 // C++ implementation of the approach
80 // C++ implementation of the approach
5 using
75
// C++ implementation of the approach
7
// C++ implementation of the approach
8_______6_______ using
79using
80_______3_______4
// C++ implementation of the approach
8_______2_______83#include
02 using
85
// C++ implementation of the approach
8
// C++ implementation of the approach
8_______0_______56
// C++ implementation of the approach
8_______0_______58
// C++ implementation of the approach
8_______0_______60
// C++ implementation of the approach
8
// C++ implementation of the approach
8// C++ implementation of the approach
62
// C++ implementation of the approach
8// C++ implementation of the approach
64
// C++ implementation of the approach
8// C++ implementation of the approach
66
const
3
const
3
namespace
02
Python3
namespace
03
namespace
04 namespace
05_______0_______72 namespace
07
namespace
08 namespace
09 // C++ implementation of the approach
83namespace
4
namespace
12
namespace
13
namespace
14namespace
09 namespace
16
namespace
17
namespace
18 namespace
19
// C++ implementation of the approach
8namespace
21
// C++ implementation of the approach
8_______3_______23namespace
09 namespace
25#include
84namespace
27namespace
28#include
8namespace
08 using
57 #include
05using
69
// C++ implementation of the approach
8_______3_______35namespace
09 namespace
08namespace
4
// C++ implementation of the approach
8_______1_______7 namespace
41_______3_______42 namespace
43#include
8#include
16namespace
88_______2_______57 #include
05namespace
51