Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include
- #include
- using namespace std;
- ifstream fin("cmmdc.in");
- #define NMax 20
- unsigned a[NMax], n;
- void Citire()
- {
- fin>>n;
- for (int i=0; i>a;
- fin.close();
- }
- unsigned Euclid(unsigned x, unsigned y)
- {
- //calculeaza cmmdc(x, y) prin algoritmul lui Euclid
- unsigned r;
- while (y)
- {
- r = x % y;
- x = y;
- y = r;
- }
- return x;
- }
- unsigned cmmdc(unsigned p, unsigned q)
- {
- int m;
- //functia intoarce cmmdc(a[p], ..., a[q])
- if (q-p <= 1) //cmmdc se poate calcula direct
- return Euclid(a[p], a[q]);
- m = (p + q) / 2;
- return Euclid(cmmdc(p, m), cmmdc(m+1, q));
- }
- int main ()
- {
- Citire();
- cout<<"cmmdc= "<<cmmdc(0, n-1)<<'\n';
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement