2. zadatak

 avatar
unknown
plain_text
3 years ago
881 B
4
Indexable
#include <iostream>
#include <cmath>
#include <stdexcept>

bool Prost(int broj) 
{
    if(broj < 2) return false;
    if(broj == 2) return true;
    for(int i = 2; i*i <= broj; i++) {         //i <= broj/i
        if(broj%i==0) return false;
        if(i*i>broj) return true;
    }
    return true;
}
bool Goldbach(int n, int &p, int &q) 
{
    for(int i = 2; i<=n; i++) {
        p = i;
        if(Prost(p)) {
            q = n - p;
        }
        if(Prost(q)) {
            return true;
            break;
        }
        else std::logic_error("Rastava ne postoji!");
    }

}
int main ()
{ 
    std::cout << "Unesi broj: ";
    int broj;
    std::cin >> broj;
    int x, y;
    if(Goldbach(broj,x,y)) {
        std::cout << broj << " je zbir prostih brojeva " << x << " i " << y;
    } else {
        std::cout << broj << " nije zbir dva prosta broja!";
    }
	return 0;
}