This is a solution to FonnD's Puzzle Involving Squares, written in portable (hah) C99.
You can compile it like this: gcc puzzle.c -o puzzle -std=c99 -Wall -pedantic
#include <stdio.h>
#define POWER2(x) ((x)*(x));
#define MIN(x,y) ((x) < (y) ? (x) : (y))
int square(int x) {
if (x < 0) {
return -1;
}
int result = 0;
for (int i = 0; i < x; i++) {
result += POWER2(x-i);
}
return result;
}
int rectangle(int x, int y) {
if (x < 0 || y < 0) {
return -1;
}
int result = 0;
for (int i = 0; i <= MIN(x,y); i++) {
result += (x-i)*(y-i);
}
return result;
}
int cube(int x) {
if (x < 0) {
return -1;
}
return 3 * (x+1) * square(x);
}
int cuboid(int x, int y, int z) {
if (x < 0 || y < 0 || z < 0) {
return -1;
}
return (z+1) * rectangle(x,y) + (y+1) * rectangle(x,z) + (x+1) * rectangle(y,z);
}
int main() {
printf("%d\n", square(3));
printf("%d\n", rectangle(3,4));
printf("%d\n", cube(3));
printf("%d\n", cuboid(3,4,5));
return 0;
}
Made by strata, 03feb18 10:05am. Written in the Editor of the Beast.