Avatar of 4d47

4d47's solution

to Grains in the Erlang Track

Published at Jul 22 2018 · 0 comments
Test suite


This exercise has changed since this solution 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.
  • Optimize for readability.

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?

Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit

Test versioning

Each problem defines a macro TEST_VERSION in the test file and verifies that the solution defines and exports a function test_version returning that same value.

To make tests pass, add the following to your solution:


test_version() ->

The benefit of this is that reviewers can see against which test version an iteration was written if, for example, a previously posted solution does not solve the current problem or passes current tests.


For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.


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.




-define(assertGrains(Grains, Square), ?assertMatch(Grains, grains:square(Square))).

square_1_test() ->
  ?assertGrains(1, 1).

square_2_test() ->
  ?assertGrains(2, 2).

square_3_test() ->
  ?assertGrains(4, 3).

square_4_test() ->
  ?assertGrains(8, 4).

square_16_test() ->
  ?assertGrains(32768, 16).

square_32_test() ->
  ?assertGrains(2147483648, 32).

square_64_test() ->
  ?assertGrains(9223372036854775808, 64).

total_grains_test() ->
  ?assertMatch(18446744073709551615, grains:total()).

version_test() ->
  ?assertMatch(1, grains:test_version()).

-export([square/1, total/0, test_version/0]).

square(1) ->
square(N) ->
    2 * square(N - 1).

total() ->
    lists:sum([square(X) || X <- lists:seq(1, 64)]).

test_version() ->

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?
  • Are there new concepts here that you could read more about to improve your understanding?