Abdulg

GCD Array (Sedgewick)

Sep 2nd, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #define WIDTH 50
  3.  
  4. int EuclidGCD(int A, int B);
  5.  
  6. int main()
  7. {
  8.     int a[WIDTH][WIDTH];
  9.  
  10.     int i,j;
  11.    
  12.     for (i = 0; i < WIDTH; i++)
  13.     {
  14.         for (j = 0; j < WIDTH; j++)
  15.         {
  16.             if (EuclidGCD(i,j) == 1)
  17.                 a[i][j] = 1;
  18.  
  19.             else
  20.                 a[i][j] = 0;
  21.         }
  22.     }
  23.  
  24.     int x,y;
  25.     for (y = 0; y < WIDTH; y++)
  26.     {
  27.         for (x = 0; x < WIDTH; x++)
  28.             printf("%d, ", a[x][y]);
  29.         printf("\n");
  30.     }
  31.    
  32.     return 0;
  33. }
  34.  
  35. int EuclidGCD(int A, int B)
  36. {
  37.     if (A + B == A || A + B == B) return A + B;
  38.  
  39.     while (A != B)
  40.     {
  41.         if (B > A)
  42.         {
  43.             int Temp = A;
  44.             A = B;
  45.             B = Temp;
  46.         }
  47.         A = A - B;
  48.     }
  49.  
  50.     return A;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment