In mathematics, the Euclidean algorithm, or Euclid’s algorithm, is an efficient method for computing the greatest common divisor (GCD) of two numbers, the largest number that divides both of them without leaving a remainder. It is named after the ancient Greek mathematician Euclid, who first described it in Euclid’s Elements (c. 300 BC). It is an example of an algorithm, a step-by-step procedure for performing a calculation according to well-defined rules, and is one of the oldest algorithms in common use. It can be used to reduce fractions to their simplest form, and is a part of many other number-theoretic and cryptographic calculations.
// filename: algoritmulEuclidIntregi.cpp
// borland c++ 5.0
// algoritmul lui Euclid pentru numere intregi
// marius ionescu, november 12, 2005
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int cmmdc(long int,long int);
void ecran(void);
void ecran()
{ clrscr();
gotoxy(10,10);
cout<<"(1) Algoritmul lui Euclid";
gotoxy(10,12);
cout<<"(2) Iesire din program";
gotoxy(10,14);
cout<<"Optiunea= ";
}
int cmmdc(long int a,long int b)
{ int temp;
if(a==0&&b!=0) return a;
if(a!=0&&b==0) return b;
if(a==0&&b==0) return 0;
if(b>a)
{ temp=a;
a=b;
b=temp;
}
while(a%b)
{ temp=a%b;
a=b;
b=temp;
}
return b;
}
void main()
{ int option;
float number1,number2; // protectie la introducere de reali
while(1)
{ ecran();cin>>option;clrscr();
if(option==2) exit(1);
cout<<"Dai primul numar: "; cin>>number1;
if(number1!=floor(number1)) {cout<<"acesta nu este un numar intreg!!!!"; getch(); exit(1); } // pt reali
cout<<"Dai al doilea numar: "; cin>>number2;
if(number1!=floor(number1)) {cout<<"acesta nu este un numar intreg!!!!"; getch(); exit(1); } // pt reali
cout<<"\nCMMDC este: "<<cmmdc(number1,number2)<<endl; // se permite doar transmiterea de valori
// intregi prin cele doua conditii
cout<<"\napasa o tasta!";
getch();
}
}
412,655 total views, 1 views today
























