APL/APL1.1/TP07/figures.c

64 lines
1.4 KiB
C

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int sq(int x) {
return x*x;
}
int getmaxsq(int value) {
int max_square = 0;
for (int i = 0; i <= value; i++) {
if ( value - sq(i) >= 0) max_square = i;
else break;
}
return max_square;
}
void printr(int value, int v1, int v2, int v3, int v4) {
printf("%d = %d + %d + %d + %d\n", value, sq(v1), sq(v2), sq(v3), sq(v4));
}
int main(int argc, char * argv[]) {
int value;
printf("Veuillez donner un entier naturel : ");
scanf("%d", &value);
int ms1 = getmaxsq(value);
for (int i1 = ms1; i1 > 0 && sq(i1)*4 >= value && value - sq(i1) < sq(i1+1); i1--) {
int val1 = value - sq(i1);
if (val1 == 0) printr(value, i1, 0, 0, 0);
else {
int ms2 = getmaxsq(val1);
for (int i2 = ms2; i2 > 0 && sq(i2)*4 >= val1 && val1 - sq(i2) < sq(i2+1); i2--) {
int val2 = val1 - sq(i2);
if (val2 == 0) printr(value, i1, i2, 0, 0);
else {
int ms3 = getmaxsq(val2);
for (int i3 = ms3; i3 > 0 && sq(i3)*4 >= val2 && val2 - sq(i3) < sq(i3+1); i3--) {
int val3 = val2 - sq(i3);
if (val3 == 0) printr(value, i1, i2, i3, 0);
else {
int ms4 = getmaxsq(val3);
for (int i4 = ms4; i4 > 0 && sq(i4)*4 >= val3 && val3 - sq(i4) < sq(i4+1); i4--) {
int val4 = val3 - sq(i4);
if (val4 == 0) printr(value, i1, i2, i3, i4);
}
}
}
}
}
}
}
return EXIT_SUCCESS;
}