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

SergiiVlasiuk's solution

to Grains in the Scala Track

Published at Aug 23 2019 · 0 comments
Test suite

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?

The Scala exercises assume an SBT project scheme. The exercise solution source should be placed within the exercise directory/src/main/scala. The exercise unit tests can be found within the exercise directory/src/test/scala.

To run the tests simply run the command sbt test in the exercise directory.

For more detailed info about the Scala track see the help page.


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.


import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
class GrainsTest extends FunSuite with Matchers {

  test("1") {
    Grains.square(1) should be(Some(1))

  test("2") {
    Grains.square(2) should be(Some(2))

  test("3") {
    Grains.square(3) should be(Some(4))

  test("4") {
    Grains.square(4) should be(Some(8))

  test("16") {
    Grains.square(16) should be(Some(32768))

  test("32") {
    Grains.square(32) should be(Some(BigInt("2147483648")))

  test("64") {
    Grains.square(64) should be(Some(BigInt("9223372036854775808")))

  test("square 0 raises an exception") {
    Grains.square(0) should be(None)

  test("negative square raises an exception") {
    Grains.square(-1) should be(None)

  test("square greater than 64 raises an exception") {
    Grains.square(65) should be(None)

  test("returns the total number of grains on the board") {
    Grains.total should be(BigInt("18446744073709551615"))
object Grains {
  def square(squareNumber: Int): Option[BigInt] = squareNumber - 1 match {
    case x if x < 0 | x > 63 => None
    case x => Some(BigInt(1) << x)
    //case x => Some(BigInt(2).pow(x))

  def total: BigInt = (BigInt(1) << 64) - 1

  //def total: BigInt = (1 to 64).flatMap(square).sum
  //def total: BigInt = BigInt(2).pow(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?