Advertisement
Guest User

Berechnungsvergleich

a guest
Sep 25th, 2018
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void gcd(int m, int n){ // euklid algorithm
  5. int r;
  6. static int calc_num = 0; // modulo zaehler
  7.  
  8. if(n == 0){
  9. printf("GCD: %d\n", m);
  10. printf("Calculations: %d\n", calc_num);
  11. calc_num = 0; // zaehler zuruecksetzen
  12. return;
  13. }
  14. r = m % n;
  15. calc_num++; // zaehler inkrementieren
  16. gcd(n, r);
  17. }
  18.  
  19. void gcd2(int m, int n){ // consecutive integer checking algorithm
  20. int t;
  21. int calc_num = 0; // modulo zaehler
  22.  
  23. if(m < n){
  24. t = m;
  25. }
  26. else{
  27. t = n;
  28. }
  29.  
  30. while(t > 0){
  31. calc_num++; // zaehler inkrementieren
  32. if(m % t == 0){
  33. calc_num++; // zaehler inkrementieren
  34. if(n % t == 0){
  35. printf("GCD: %d\n", t);
  36. printf("Calculations: %d\n", calc_num);
  37. return;
  38. }
  39. }
  40. t--;
  41. }
  42. }
  43.  
  44. int main(){
  45. srand(time(NULL));
  46.  
  47. int m = 100000 + rand() % (200000 - 100000 + 1);
  48. int n = 100000 + rand() % (200000 - 100000 + 1);
  49.  
  50. printf("m: %d, n: %d\n", m, n);
  51.  
  52. printf("Euclid algorithm:\n");
  53. gcd(m, n);
  54.  
  55. printf("Consecutive integer checking algorithm:\n");
  56. gcd2(m, n);
  57.  
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement