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:
Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:
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?
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
Each problem defines a macro
TEST_VERSION in the test file and
verifies that the solution defines and exports a function
returning that same value.
To make tests pass, add the following to your solution:
-export([test_version/0]). test_version() -> 1.
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
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
-module(grains_tests). -include_lib("erl_exercism/include/exercism.hrl"). -include_lib("eunit/include/eunit.hrl"). -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()).
-module(grains). -export([square/1, total/0, test_version/0]). % square(1) -> 1; square(N) -> 2 * square(N - 1). total() -> lists:sum([square(X) || X <- lists:seq(1, 64)]). test_version() -> 1.
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.