fork download
  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5. #include <string.h>
  6.  
  7. #include <stdbool.h>
  8.  
  9. int SimplifySquareRoot(int a, int *x, int *y){
  10. int d, s;
  11. *y = 1;
  12. *x = a;
  13. d = 4;
  14. s = 2;
  15. while (a >= d){
  16. while (a % d == 0){
  17. a /= d;
  18. *x = a;
  19. *y *= s;
  20. }
  21. d = d + 2 * s + 1;
  22. s++;
  23. }
  24. }
  25.  
  26. int GetSquareRoot(int a, int *x, int *y, bool *NoSol){
  27. if (a < 0)
  28. *NoSol = true;
  29. else if (a == 0){
  30. *y = 0;
  31. *x = 0;
  32. }
  33. else
  34. SimplifySquareRoot(a, x, y);
  35. }
  36.  
  37. void PrintSolution(int *x, int *y, bool *NoSol){
  38. if (*NoSol)
  39. printf("No Solutions");
  40. else
  41. if (*x == 0)
  42. printf("x = %d\n", 0);
  43. else
  44. if (*x == 1)
  45. printf("x = +- %d\n", y);
  46. else
  47. if (*y == 1)
  48. printf("x = +- Sqrt( %d %s\n", x, ")");
  49. else
  50. printf("x = +- %d %s %d %s\n", y, "Sqrt(", x, ")");
  51. }
  52.  
  53. int main() {
  54. int a, x = 0, y = 0;
  55. bool Solutions;
  56. printf("a = ");
  57. scanf("%d\n", &a);
  58.  
  59. GetSquareRoot(a, &x, &y, &Solutions);
  60.  
  61. PrintSolution(&x, &y, &Solutions);
  62.  
  63. return 0;
  64. }
  65.  
Success #stdin #stdout 0s 5524KB
stdin
5
stdout
a = No Solutions