dmilicev

perfect_squares_and_cubes_v1.c

Sep 7th, 2020
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. /*
  2.  
  3.     perfect_squares_and_cubes_v1.c
  4.  
  5.     Task:
  6.     a,b,c,d,e are five consecutive positive numbers such that
  7.     (b+c+d) is a perfect square and
  8.     (a+b+c+d+e) is a perfect cube.
  9.     Find the smallest possible  values  of such  numbers.
  10.  
  11.  
  12.     You can find all my C programs at Dragan Milicev's pastebin:
  13.  
  14.     https://pastebin.com/u/dmilicev
  15.  
  16. */
  17.  
  18. #include <stdio.h>
  19.  
  20. // return square root of number if given number is perfect square, otherwise return 0.
  21. int is_perfect_square(int number)
  22. {
  23.     int i;
  24.  
  25.     for(i=0; i<=number/4+1; i++)
  26.         if(number == i*i)
  27.             return i;
  28.  
  29.     return 0;
  30. }
  31.  
  32. // return cube root of number if given number is perfect cube, otherwise return 0.
  33. int is_perfect_cube(int number)
  34. {
  35.     int i;
  36.  
  37.     for(i=0; i<=number/8+1; i++)
  38.         if(number == i*i*i)
  39.             return i;
  40.  
  41.     return 0;
  42. }
  43.  
  44.  
  45. int main(void)
  46. {
  47.     int a, number_of_solutions=0, number_of_iterations=0;
  48. /*
  49.     a,  b,   c,   d,   e    are five consecutive positive numbers
  50.  
  51.     a, a+1, a+2, a+3, a+4
  52.  
  53.     b + c + d = a+1 + a+2 + a+3 = 3*a+6
  54.  
  55.     a + b + c + d = a + a+1 + a+2 + a+3 + a+4 = 5*a+10
  56. */
  57.  
  58.     for(a=1; a<700; a++)
  59.     {
  60.         number_of_iterations++;
  61.  
  62.         if( is_perfect_square(3*a+6) && is_perfect_cube(5*a+10) )
  63.         {
  64.             number_of_solutions++;
  65.             printf("\n  a   b   c   d   e \n");
  66.             printf(" %3d %3d %3d %3d% 3d \n", a, a+1, a+2, a+3, a+4 );
  67.         }
  68.     }
  69.  
  70.     if( number_of_solutions == 0 )
  71.         printf("\n There is no such numbers. \n");
  72.     else
  73.         printf("\n\n There are %d such numbers. \n", number_of_solutions );
  74.  
  75.     printf("\n Number of iterations %d. \n", number_of_iterations );
  76.  
  77.     return 0;
  78.  
  79. } // main()
  80.  
Add Comment
Please, Sign In to add comment