🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of JorgeDS

JorgeDS's solution

to Grains in the Erlang Track

Published at Mar 02 2021 · 0 comments
Instructions
Test suite
Solution

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

Questions?

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.

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.

grains_tests.erl

%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/426b93d05abb69f6a7b5d84b61ba4b6fcd1e6385/exercises/grains/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(grains_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").




'1_square_grains_on_square_1_test_'() ->
    {"Square grains on square 1",
     ?_assertMatch(1, grains:square(1))}.

'2_square_grains_on_square_2_test_'() ->
    {"Square grains on square 2",
     ?_assertMatch(2, grains:square(2))}.

'3_square_grains_on_square_3_test_'() ->
    {"Square grains on square 3",
     ?_assertMatch(4, grains:square(3))}.

'4_square_grains_on_square_4_test_'() ->
    {"Square grains on square 4",
     ?_assertMatch(8, grains:square(4))}.

'5_square_grains_on_square_16_test_'() ->
    {"Square grains on square 16",
     ?_assertMatch(32768, grains:square(16))}.

'6_square_grains_on_square_32_test_'() ->
    {"Square grains on square 32",
     ?_assertMatch(2147483648, grains:square(32))}.

'7_square_grains_on_square_64_test_'() ->
    {"Square grains on square 64",
     ?_assertMatch(9223372036854775808, grains:square(64))}.

'8_square_0_raises_an_exception_test_'() ->
    {"square 0 raises an exception",
     ?_assertMatch({error,
		    "square must be between 1 and 64"},
		   grains:square(0))}.

'9_negative_square_raises_an_exception_test_'() ->
    {"negative square raises an exception",
     ?_assertMatch({error,
		    "square must be between 1 and 64"},
		   grains:square(-1))}.

'10_square_greater_than_64_raises_an_exception_test_'() ->
    {"square greater than 64 raises an exception",
     ?_assertMatch({error,
		    "square must be between 1 and 64"},
		   grains:square(65))}.

'11_returns_the_total_number_of_grains_on_the_board_test_'() ->
    {"returns the total number of grains on "
     "the board",
     ?_assertMatch(18446744073709551615, grains:total())}.
-module(grains).

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

square(_Square) when _Square >= 1, _Square =< 64 ->  1 bsl (_Square- 1);
square(_Square) -> {error,"square must be between 1 and 64"}.

total() ->  (1 bsl 64) - 1.

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?