diff --git a/solutions/c/grains/1/grains.c b/solutions/c/grains/1/grains.c new file mode 100644 index 0000000..e2c07b7 --- /dev/null +++ b/solutions/c/grains/1/grains.c @@ -0,0 +1,20 @@ +#include "grains.h" + +uint64_t square(uint8_t index) +{ + if (index == 1) + return 1; + return (2 * square(index - 1)); +} + +uint64_t total(void) +{ + /* + S_n = 1 + 2 + 4 + ... + 2^n + S_n * 2 = 2 + 4 + ... + 2^(n + 1) + --> S_n = 2^(n + 1) - 1 + so S_64 = 1 + 2 + 4 + ... + 2^64 = 2^(65) - 1 + */ + + return (square(65) - 1); +} \ No newline at end of file diff --git a/solutions/c/grains/1/grains.h b/solutions/c/grains/1/grains.h new file mode 100644 index 0000000..ed53556 --- /dev/null +++ b/solutions/c/grains/1/grains.h @@ -0,0 +1,9 @@ +#ifndef GRAINS_H +#define GRAINS_H + +#include + +uint64_t square(uint8_t index); +uint64_t total(void); + +#endif