Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- using namespace std;
- int st[51], n, p, primes[51];
- ofstream fout("prime.out");
- int check(int x) {
- for (int d = 2; d * d <= x; d++)
- if (x % d == 0)
- return 0;
- return 1;
- }
- void gen() {
- int cp = 0;
- for (int x = 11; cp < p; x += 2)
- if (check(x))
- primes[++cp] = x;
- }
- void tipar(int k) {
- for (int i = 1; i <= k; i++)
- fout << primes[st[i]] << ' ';
- fout << endl;
- }
- int solutie(int k) {
- return k == p;
- }
- int validare(int k) {
- for (int i = 1; i < k; i++)
- if (st[i] == st[k])
- return 0;
- return 1;
- }
- void bktr() {
- int k = 1;
- st[k] = 0;
- while (k) {
- if (st[k] < n) {
- st[k]++;
- if (validare(k)) {
- if (solutie(k))
- tipar(k);
- else {
- k++;
- st[k] = 0;
- }
- }
- }
- else
- k--;
- }
- }
- int main() {
- cin >> n >> p;
- gen();
- swap(n, p);
- bktr();
- return 0;
- }
Add Comment
Please, Sign In to add comment