fork download
  1. /* Program Fermat's factorisation algorithm to factor any odd n into two factors.
  2. Code it as a function with argument n. Show your code.*/
  3.  
  4. #include <stdio.h>
  5. //#include <conio.h>
  6. #include <math.h>
  7.  
  8.  
  9.  
  10. int FERMAT_factors_oddonly (int y);
  11. bool Check_square (int z);
  12.  
  13.  
  14. int main (){
  15. long int g;
  16. printf("Please input ODD integer:\n");
  17. scanf("%ld",&g);
  18.  
  19. FERMAT_factors_oddonly(g);
  20.  
  21. getch();
  22. }
  23.  
  24. int FERMAT_factors_oddonly (int n){
  25. int a=0;
  26. double B; // B = b*b;
  27. a = ceil (sqrt (n));
  28. B = (a*a) - n;
  29.  
  30. printf("\nINPUT NUMBER: %d\n",n);
  31.  
  32. int c = 0;
  33.  
  34. while (!Check_square(B)){
  35.  
  36. B = pow((a+c),2) - n;
  37.  
  38. printf(" \n ::: Finding B ::: \n");
  39. printf("( %d + %d )^2 - %d = %.2f\n",a,c,n,B);
  40.  
  41. switch (Check_square(B)){
  42. case 0: printf(" \n %.2f is NOT a perfect square !!!\n",B);
  43. break;
  44. case 1: printf(" \n %.2f IS A PERFECT SQUARE \\: BINGO :/ \n", B);
  45. break;
  46. }
  47. printf("\n");
  48. //getch(); // step by step user observe output
  49. c++;
  50. }
  51.  
  52. if (c!=0) c--; // to ensure the c value doesn't increased after the last run for one time factorisation
  53.  
  54. long int factrA, factrB;
  55.  
  56. factrA = a+c;
  57. factrB = sqrt(B);
  58.  
  59. printf("FULL EQUATION ::: N = (a)^2 - (b)^2\n",n,factrA,factrB);
  60. printf("%19d = (%d)^2 - (%d)^2\n",n,factrA,factrB);
  61. printf("Thus, value of a = %d, b = %d\n\n",factrA,factrB);
  62. printf("::: The factors are ::: ( %d + %d ) and ( %d - %d )",factrA,factrB,factrA,factrB );
  63. printf(" = ( %d ) and ( %d )",factrA+factrB,factrA-factrB );
  64.  
  65.  
  66. }
  67.  
  68. bool Check_square (int z){
  69.  
  70. if ((sqrt(z)*sqrt(z)) == z || ((sqrt(z)+1)*(sqrt(z)+1) == z))
  71.  
  72. return 1;
  73.  
  74. }
  75.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
03303b790fb149da3406d495ab9b9fb8a9e293445e3bd43b18ef2f0521b726ebe8d838ba774bb5240f08f7fbca0a142a1d4a61ea973294e684a8d1a2cdf18a84f2db7099b8e977588b0b891292558caa05cf5df2bc6334c5ee5083a234edfc79a95c478a78e337c723ae8834fb8a9931b74503ffea9e61bf53d8716984ac47837b
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:21:8: error: ‘getch’ was not declared in this scope
  getch();
        ^
stdout
Standard output is empty