commit 2856722710991b8fd49639a3c86b4fd242bec01f
parent 764270694f2c0521e38495f0e0eadbb2d5c6187a
Author: dwrz <dwrz@dwrz.net>
Date: Sat, 11 May 2019 20:17:39 +0000
Add number of squares constant in grains
For clarity.
Diffstat:
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/c/grains/src/grains.c b/c/grains/src/grains.c
@@ -1,11 +1,13 @@
#include "grains.h"
+const int NUMBER_OF_SQUARES = 64;
+
// square returns the number of grains to be placed at the nth square.
// An unsigned long long is necessary to hold the value of 2^64, which
// on 64-bit is equivalent to the max value of that type (ULLONG_MAX).
// square returns 0 for non-existing squares.
unsigned long long square(int n) {
- if (n < 1 || n > 64) return 0;
+ if (n < 1 || n > NUMBER_OF_SQUARES) return 0;
// A left shift of 0 leaves a number unchanged.
// Otherwise, each left shift of 1 doubles the number;
// i.e, 1 << 0 = 1, 1 << 1 = 2, 1 << 2 = 4, etc.
@@ -18,7 +20,7 @@ unsigned long long square(int n) {
unsigned long long total() {
unsigned long long n = 0;
// Add up all the squares.
- for (int i = 1; i <= 64; i++) {
+ for (int i = 1; i <= NUMBER_OF_SQUARES; i++) {
n += square(i);
}
return n;