ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# vlzware's solution

## to Grains in the C Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.

There once was a wise servant who saved the life of a prince. The king promised to pay whatever the servant could dream up. Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. One grain on the first square of a chess board. Two grains on the next. Four on the third, and so on.

There are 64 squares on a chessboard.

Write code that shows:

• how many grains were on each square, and
• the total number of grains

## For bonus points

Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:

• Optimize for speed.

Then please share your thoughts in a comment on the submission. Did this experiment make the code better? Worse? Did you learn anything from it?

## Getting Started

Make sure you have read the C page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

## Passing the Tests

Get the first test compiling, linking and passing by following the three rules of test-driven development.

The included makefile can be used to create and run the tests using the `test` task.

``````make test
``````

Create just the functions you need to satisfy any compiler errors and get the test to fail. Then write just enough code to get the test to pass. Once you've done that, move onto the next test.

As you progress through the tests, take the time to refactor your implementation for readability and expressiveness and then go on to the next test.

Try to use standard C99 facilities in preference to writing your own low-level algorithms or facilities by hand.

## Source

JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### test_grains.c

``````#include "vendor/unity.h"
#include "../src/grains.h"

void setUp(void)
{
}

void tearDown(void)
{
}

void test_square_1(void)
{
TEST_ASSERT_EQUAL_UINT64(1ul, square(1));
}

void test_square_2(void)
{
TEST_IGNORE();               // delete this line to run test
TEST_ASSERT_EQUAL_UINT64(2ul, square(2));
}

void test_square_3(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT64(4ul, square(3));
}

void test_square_4(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT64(8ul, square(4));
}

void test_square_16(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT64(32768ul, square(16));
}

void test_square_32(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT64(2147483648ul, square(32));
}

void test_square_64(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT64(9223372036854775808ul, square(64));
}

void test_square_0_does_not_exist(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT64(0, square(0));
}

void test_square_greater_than_64_does_not_exist(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT64(0, square(65));
}

void test_total(void)
{
TEST_IGNORE();
TEST_ASSERT_EQUAL_UINT64(18446744073709551615ul, total());
}

int main(void)
{
UnityBegin("test/test_grains.c");

RUN_TEST(test_square_1);
RUN_TEST(test_square_2);
RUN_TEST(test_square_3);
RUN_TEST(test_square_4);
RUN_TEST(test_square_16);
RUN_TEST(test_square_32);
RUN_TEST(test_square_64);
RUN_TEST(test_square_0_does_not_exist);
RUN_TEST(test_square_greater_than_64_does_not_exist);
RUN_TEST(test_total);

UnityEnd();
return 0;
}``````

### makefile

``````CFLAGS  = -std=c99
CFLAGS += -g
CFLAGS += -Wall
CFLAGS += -Wextra
CFLAGS += -pedantic
CFLAGS += -Werror

VFLAGS  = --quiet
VFLAGS += --tool=memcheck
VFLAGS += --leak-check=full
VFLAGS += --error-exitcode=1

test: tests.out
@./tests.out

memcheck: tests.out
@valgrind \$(VFLAGS) ./tests.out
@echo "Memory check passed"

clean:
rm -rf *.o *.out *.out.dSYM

tests.out: test/test_grains.c src/grains.c src/grains.h
@echo Compiling \$@
@cc \$(CFLAGS) src/grains.c test/vendor/unity.c test/test_grains.c -o tests.out -lm``````

### src/grains.c

``````/* https://en.wikipedia.org/wiki/Wheat_and_chessboard_problem */

#include <math.h>
#include <stdint.h>
#define BOARD 64

uint64_t square(int which)
{
if (which <= 0 || which > 64)
return 0;

return (uint64_t) pow(2, which - 1);
}

uint64_t total(void) {
return (uint64_t) (pow(2, BOARD) - 1);
}``````

### src/grains.h

``````#ifndef GRAINS_H
#define GRAINS_H

#include <stdint.h>

uint64_t square(int which);
uint64_t total(void);

#endif``````

Solution Author
commented over 2 years ago

-lm is in the makefile because of math.h

Note: there are solutions here with bit shifting which are better.

### What can you learn from this solution?

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?