Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- perfect_squares_and_cubes_v1.c
- Task:
- a,b,c,d,e are five consecutive positive numbers such that
- (b+c+d) is a perfect square and
- (a+b+c+d+e) is a perfect cube.
- Find the smallest possible values of such numbers.
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- // return square root of number if given number is perfect square, otherwise return 0.
- int is_perfect_square(int number)
- {
- int i;
- for(i=0; i<=number/4+1; i++)
- if(number == i*i)
- return i;
- return 0;
- }
- // return cube root of number if given number is perfect cube, otherwise return 0.
- int is_perfect_cube(int number)
- {
- int i;
- for(i=0; i<=number/8+1; i++)
- if(number == i*i*i)
- return i;
- return 0;
- }
- int main(void)
- {
- int a, number_of_solutions=0, number_of_iterations=0;
- /*
- a, b, c, d, e are five consecutive positive numbers
- a, a+1, a+2, a+3, a+4
- b + c + d = a+1 + a+2 + a+3 = 3*a+6
- a + b + c + d = a + a+1 + a+2 + a+3 + a+4 = 5*a+10
- */
- for(a=1; a<700; a++)
- {
- number_of_iterations++;
- if( is_perfect_square(3*a+6) && is_perfect_cube(5*a+10) )
- {
- number_of_solutions++;
- printf("\n a b c d e \n");
- printf(" %3d %3d %3d %3d% 3d \n", a, a+1, a+2, a+3, a+4 );
- }
- }
- if( number_of_solutions == 0 )
- printf("\n There is no such numbers. \n");
- else
- printf("\n\n There are %d such numbers. \n", number_of_solutions );
- printf("\n Number of iterations %d. \n", number_of_iterations );
- return 0;
- } // main()
Add Comment
Please, Sign In to add comment