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

radmen's solution

to Grains in the Elixir Track

Published at Jun 09 2020 · 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

Execute the tests with:

$ mix test

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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_test.exs

defmodule GrainsTest do
  use ExUnit.Case

  # @tag :pending
  test "square 1" do
    assert Grains.square(1) === {:ok, 1}
  end

  @tag :pending
  test "square 2" do
    assert Grains.square(2) === {:ok, 2}
  end

  @tag :pending
  test "square 3" do
    assert Grains.square(3) === {:ok, 4}
  end

  @tag :pending
  test "square 4" do
    assert Grains.square(4) === {:ok, 8}
  end

  @tag :pending
  test "square 16" do
    assert Grains.square(16) === {:ok, 32768}
  end

  @tag :pending
  test "square 32" do
    assert Grains.square(32) === {:ok, 2_147_483_648}
  end

  @tag :pending
  test "square 64" do
    assert Grains.square(64) === {:ok, 9_223_372_036_854_775_808}
  end

  @tag :pending
  test "total grains" do
    assert Grains.total() === {:ok, 18_446_744_073_709_551_615}
  end

  @tag :pending
  test "square greater than 64 returns an error" do
    assert Grains.square(65) ===
             {:error, "The requested square must be between 1 and 64 (inclusive)"}
  end

  @tag :pending
  test "negative square returns an error" do
    assert Grains.square(-1) ===
             {:error, "The requested square must be between 1 and 64 (inclusive)"}
  end

  @tag :pending
  test "square 0 returns an error" do
    assert Grains.square(0) ===
             {:error, "The requested square must be between 1 and 64 (inclusive)"}
  end
end

test_helper.exs

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule Grains do
  @doc """
  Calculate two to the power of the input minus one.
  """
  @spec square(pos_integer) :: {:ok, pos_integer} | {:error, String.t()}
  def square(number)
      when number < 1
      when number > 64 do
    {:error, "The requested square must be between 1 and 64 (inclusive)"}
  end

  def square(number) do
    {:ok, value_of_square(number)}
  end

  defp value_of_square(number) do
    :math.pow(2, number - 1)
    |> trunc()
  end

  @doc """
  Adds square of each number from 1 to 64.
  """
  @spec total :: {atom, pos_integer}
  def total do
    total =
      1..64
      |> Enum.reduce(0, &(&2 + value_of_square(&1)))

    {:ok, total}
  end
end

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?