This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: { //Requirements assert(j>0); assert(i>=0); int g=j; while(not((i%g)==0 and (j%g)==0 )) { g=g1; } return g; } //=========================================================== //gcd3.cpp //Computes the greatest common divisor of two nonnegative //integers a and b without dividing numbers //=========================================================== #include <cassert> using namespace std; int gcd(int a, int b) { assert(not(a==0 and b==0));//they can't both be zero assert(a>=0); assert(b>=0); int i=a;//make local copies int j=b; while((i!=j) and (j!=0) and (i!=0)) { //loop invariant gcd(i,j)=gcd(a,b) if(i>j) { i=ij;//reduce i but leave GCD unchanged } else { j=ji;//reduce j but leave GCD unchanged } } //Now only special cases are possible hiLo…………and the gcd’s if(i==0) //GCD(0,j)=j { return j; } return i; //GCD(i,i)=i or GCD(i,0)=i }...
View
Full Document
 '09
 Greatest common divisor, main(void), int gcd

Click to edit the document details