Advertisement
Falexom

Untitled

Apr 9th, 2023 (edited)
928
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. int gcd(int a, int b)
  6. {
  7.     while ((a != 0) && (b != 0)) {
  8.         if (a > b) {
  9.             a = a % b;
  10.         }
  11.         else {
  12.             b = b % a;
  13.         }
  14.     }
  15.     return(a + b);
  16. }
  17.  
  18. bool isDivisible(int a, int b)
  19. {
  20.     if (a % b == 0) {
  21.         return true;
  22.     }
  23.  
  24.     return false;
  25. }
  26.  
  27. int* extended_gcd(int a, int b)
  28. {
  29.     int* ret = (int*)malloc(sizeof(int) * 3);
  30.     int x1, y1, x, y;
  31.  
  32.     if (a == 0) {
  33.         ret[0] = b;
  34.         ret[1] = 0;
  35.         ret[2] = 1;
  36.     }
  37.     else {
  38.         int temp_a = b % a;
  39.         int* temp = extended_gcd(temp_a, a);
  40.         y1 = temp[2];
  41.         x1 = temp[1];
  42.         x = y1 - (b / a) * x1;
  43.         y = x1;
  44.         ret[0] = temp[0];
  45.         ret[1] = x;
  46.         ret[2] = y;
  47.         free(temp);
  48.     }
  49.     return ret;
  50. }
  51.  
  52.  
  53. int main()
  54. {
  55.     int a, b, c, d;
  56.     bool isSolving;
  57.     int* ret = (int*)malloc(sizeof(int) * 3);
  58.  
  59.     printf("\n%s", "Input a ");
  60.     scanf("%d", &a);
  61.     printf("\n%s", "Input b ");
  62.     scanf("%d", &b);
  63.     printf("\n%s", "Input c ");
  64.     scanf("%d", &c);
  65.  
  66.     d = gcd(a, b);;
  67.     isSolving = isDivisible(c, d);
  68.     if (isSolving == false) {
  69.         printf("%s", "No solutions");
  70.         return 0;
  71.     }
  72.     else {
  73.         printf("\n%s", "Solving");
  74.         ret = extended_gcd(a, b);
  75.         printf("\nAnswer x = %d, y = %d\n", c * ret[1] / ret[0], c * ret[2] / ret[0]);
  76.     }
  77.  
  78.     return 0;
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement