Advertisement
Guest User

Untitled

a guest
May 25th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. function [prim,t2]=euclid(t,m)
  2. %algoritmul euclid extins imi calculeaza t^(-1) atunci
  3. %cand cunosc t si m, iar t si m sunt prime intre ele
  4. t=abs(t);
  5. m=abs(m);
  6. %initializare pentru cmmdc t0=0 si t1=1 => cmmdc este t
  7. r0=m;
  8. t0=0;
  9. t1=1;
  10. prim=1;
  11. while(t>1)
  12. q=floor(m/t);
  13. r=m-q*t;
  14. if(r == 0) %verific daca m/t este un nr intreg
  15. prim = 0;
  16. return; %nu are inversa
  17. end
  18. m=t;
  19. t=r;
  20. t2=mod(t0-q*t1,r0); %calculez t^(-1)
  21. t0 = t1;
  22. t1 = t2;
  23. end
  24.  
  25. function [prim,t2]=euclid(t,m)
  26. %algoritmul euclid extins imi calculeaza t^(-1) atunci
  27. %cand cunosc t si m, iar t si m sunt prime intre ele
  28. t=abs(t);
  29. m=abs(m);
  30. if(t<m)
  31. aux=t;
  32. t=m;
  33. m=aux;
  34. end
  35. %initializare pentru cmmdc t0=0 si t1=1 => cmmdc este t
  36. r0 = t;
  37. t0 = 0;
  38. t1 = 1;
  39. prim = 1;
  40. while(m>1)
  41. q=floor(t/m);
  42. r=t-q*m;
  43. if(r == 0) %verific daca t/m este un nr intreg
  44. prim = 0;
  45. return; %nu are inversa
  46. end
  47. t=m; %t devine m
  48. m=r; %m devine diferenta dintre t si raporul t/m
  49. t2 = mod(t0-q*t1,r0); %calculez t^(-1)
  50. t0 = t1;
  51. t1 = t2;
  52. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement