Algoritmo di Lepore per il calcolo del MCD
Risposte
ULTIMA VERSIONE
int MCD(a,b){//supponendo che siano stati divisi già per le potenze di 2 e di 3 e che inizialmente a >b
printf("a=%d\nb=%d\n",a,b);
while(a % 2 == 0){ a=a/2; }
while(a % 3 == 0){ a=a/3; }
while(b % 2 == 0){ b=b/2; }
while(b % 3 == 0) {b=b/3; }
if( (a % b)==0 ){
return b;
}
if( a%((a-b)/6)==0){
return (a-b)/6;
}
if ((a-b)%6==0 && a%((a-b)/6) != b ){
return MCD(a,a%((a-b)/6));
}
else{
return MCD(b,(a%b));
}
}