Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- //ifstream cin("maxd.in");
- //ofstream cout("maxd.out");
- bool Prim(int n)
- {
- if(n <= 1) return false;
- else if(n <= 3) return true;
- else if(n % 2 == 0 or n % 3 == 0) return false;
- else
- {
- for(int i = 5; i * i <= n; i += 6)
- {
- if(n % i == 0 or n % (i + 2) == 0) return false;
- }
- }
- return true;
- }
- int main()
- {
- long long a, b, nr = 0, MIN = 1e9;
- bool gasit = false;
- cin >> a >> b;
- if(a % 2 != 0)
- {
- for(int i = a; i <= b; i += 2)
- {
- int q = i;
- if(Prim(q))
- {
- gasit = true;
- if(abs((a + b) - 2 * q) < MIN)
- {
- MIN = abs((a + b) - 2 * q);
- nr = q;
- }
- else if(abs((a + b) - 2 * q) == MIN)
- {
- if(q < nr) nr = q;
- }
- }
- }
- }
- else
- {
- for(int i = a + 1; i <= b; i += 2)
- {
- int q = i;
- if(Prim(q))
- {
- gasit = true;
- if(abs((a + b) - 2 * q) < MIN)
- {
- MIN = abs((a + b) - 2 * q);
- nr = q;
- }
- else if(abs((a + b) - 2 * q) == MIN)
- {
- if(q < nr) nr = q;
- }
- }
- }
- }
- if(gasit) cout << nr;
- else cout << "NU EXISTA";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement