Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- void gcd(int m, int n){ // euklid algorithm
- int r;
- static int calc_num = 0; // modulo zaehler
- if(n == 0){
- printf("GCD: %d\n", m);
- printf("Calculations: %d\n", calc_num);
- calc_num = 0; // zaehler zuruecksetzen
- return;
- }
- r = m % n;
- calc_num++; // zaehler inkrementieren
- gcd(n, r);
- }
- void gcd2(int m, int n){ // consecutive integer checking algorithm
- int t;
- int calc_num = 0; // modulo zaehler
- if(m < n){
- t = m;
- }
- else{
- t = n;
- }
- while(t > 0){
- calc_num++; // zaehler inkrementieren
- if(m % t == 0){
- calc_num++; // zaehler inkrementieren
- if(n % t == 0){
- printf("GCD: %d\n", t);
- printf("Calculations: %d\n", calc_num);
- return;
- }
- }
- t--;
- }
- }
- int main(){
- srand(time(NULL));
- int m = 100000 + rand() % (200000 - 100000 + 1);
- int n = 100000 + rand() % (200000 - 100000 + 1);
- printf("m: %d, n: %d\n", m, n);
- printf("Euclid algorithm:\n");
- gcd(m, n);
- printf("Consecutive integer checking algorithm:\n");
- gcd2(m, n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement