Guest User

Untitled

a guest
Jul 16th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <ctype.h>
  7. #define AMAX 1000
  8. #define AMIN 3
  9.  
  10. /* this function makes the calculation of the r max and then returns r max or
  11. -1 if there was a problem in the function */
  12. int resMaxCalc (int a);
  13.  
  14. int main () {
  15. clock_t beginning, end;
  16. double tempo;
  17. beginning = clock();
  18. /* work to verify */
  19. int a, booleano, answer=0, aMin, aMax;
  20. aMin = AMIN;
  21. aMax = AMAX;
  22. /* enter cycle */
  23. for (a = aMin; a < aMax+1; a++) {
  24. booleano = resMaxCalc (a);
  25. if (booleano == -1) {
  26. printf("\nThere was a problem in the function resMaxCalc.");
  27. return 0;
  28. }
  29. answer+=booleano;
  30. }
  31. /* end of the work */
  32. end = clock();
  33. tempo = (double)(end - beginning) / CLOCKS_PER_SEC;
  34. printf("\nTime in seconds: %lf", tempo);
  35. printf("\nThe sum of r max from a=%d to a=%d is %d", aMin, aMax, answer);
  36. printf ("\n");
  37. return 0;
  38. }
  39. /******************************************************************************/
  40. int resMaxCalc (int a) {
  41. /* this function makes the calculation of the r max and then returns r max or
  42. -1 if there was a problem in the function */
  43. if (a == 0) {
  44. return -1;
  45. }
  46. int divider, left, right, leftOriginal, rightOriginal, firstR, maxR = -1, res, r;
  47. int rPrevious = -1;
  48. divider = pow (a,2);
  49. /* first values for n=1 */
  50. left = a-1;
  51. right = a+1;
  52. leftOriginal = left;
  53. rightOriginal = right;
  54. res = left + right;
  55. firstR = res % divider;
  56. if (maxR < firstR) {
  57. maxR = firstR;
  58. }
  59. /* enter infinite cycle */
  60. while (1) {
  61. /* next values */
  62. left*=leftOriginal;
  63. right*=rightOriginal;
  64. /* downsizing */
  65. left = left % divider;
  66. right = right % divider;
  67. res = left + right;
  68. r = res % divider;
  69. /* r max update */
  70. if (maxR < r) {
  71. maxR = r;
  72. } else if (r == firstR) {
  73. /* exit cycle */
  74. break;
  75. }
  76. }
  77. return maxR;
  78. }
  79. /******************************************************************************/
Add Comment
Please, Sign In to add comment