ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# bkhl's solution

## to Grains in the Bash Track

Published at Jan 05 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

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.

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?

Run the tests with:

``````bats grains_test.sh
``````

After the first test(s) pass, continue by commenting out or removing the `skip` annotations prepending other tests.

## 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.sh

``````#!/usr/bin/env bats

@test "Square 1" {
#skip
run bash grains.sh 1

[ "\$status" -eq 0 ]
[ "\$output" = "1" ]
}

@test "Square 2" {
#skip
run bash grains.sh 2

[ "\$status" -eq 0 ]
[ "\$output" = "2" ]
}

@test "Square 3" {
#skip
run bash grains.sh 3

[ "\$status" -eq 0 ]
[ "\$output" = "4" ]
}

@test "Square 4" {
#skip
run bash grains.sh 4

[ "\$status" -eq 0 ]
[ "\$output" = "8" ]
}

@test "Square 16" {
#skip
run bash grains.sh 16

[ "\$status" -eq 0 ]
[ "\$output" = "32768" ]
}

@test "Square 32" {
#skip
run bash grains.sh 32

[ "\$status" -eq 0 ]
[ "\$output" = "2147483648" ]
}

@test "Square 64" {
#skip
run bash grains.sh 64

[ "\$status" -eq 0 ]
[ "\$output" = "9223372036854775808" ]
}

@test "Square 0 causes error" {
#skip
run bash grains.sh 0

[ "\$status" -eq 1 ]
[ "\$output" = "Error: invalid input" ]
}

@test "Square -1 causes error" {
#skip
run bash grains.sh -1

[ "\$status" -eq 1 ]
[ "\$output" = "Error: invalid input" ]
}

@test "Square 65 causes error" {
#skip
run bash grains.sh 65

[ "\$status" -eq 1 ]
[ "\$output" = "Error: invalid input" ]
}``````
``````#!/bin/bash

# Print the number of grains on the given chess board square.
square() {
n="\$1"

if [[ \$n -lt 1 || \$n -gt 64 ]]; then
echo "Error: invalid input"
return 1
fi

printf %u \$((2 ** (n - 1)))
}

# Print the total number of grains on the given chess board square,
# and all the squares before it.
sum_of_squares() {
n="\$1"

printf %u \$(((2 ** n) - 1))
}

main() {
input="\$1"

if [[ \$input == total ]]; then
sum_of_squares 64
else
square "\$input" || return 1
fi
}

main "\$*" || exit 1``````