# 4d47's solution

## to Grains in the Perl 6 Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code 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?

## Resources

Remember to check out the Perl 6 documentation and resources pages for information, tips, and examples if you get stuck.

## Running the tests

There is a test suite and module included with the exercise. The test suite (a file with the extension `.t`) will attempt to run routines from the module (a file with the extension `.pm6`). Add/modify routines in the module so that the tests will pass! You can view the test data by executing the command `perl6 --doc *.t` (* being the name of the test suite), and run the test suite for the exercise by executing the command `prove6 .` in the exercise directory.

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

``````#!/usr/bin/env perl6
use v6;
use Test;
use JSON::Fast;
use lib \$?FILE.IO.dirname;
use Grains;
plan 11;

my \$c-data = from-json \$=pod.pop.contents;
for @(\$c-data<cases>[0]<cases>) {
if .<expected> == -1 {
throws-like { grains-on-square(.<input><square>) }, Exception, .<description>;
} else {
is grains-on-square(.<input><square>), |.<expected description>;
}
}
is total-grains, |\$c-data<cases>[1]<expected description>;

=begin code
{
"exercise": "grains",
"version": "1.1.0",
"The final tests of square test error conditions",
"The expectation for these tests is -1, indicating an error",
"In these cases you should expect an error as is idiomatic for your language"
],
"cases": [
{
"description": "returns the number of grains on the square",
"cases": [
{
"description": "1",
"property": "square",
"input": {
"square": 1
},
"expected": 1
},
{
"description": "2",
"property": "square",
"input": {
"square": 2
},
"expected": 2
},
{
"description": "3",
"property": "square",
"input": {
"square": 3
},
"expected": 4
},
{
"description": "4",
"property": "square",
"input": {
"square": 4
},
"expected": 8
},
{
"description": "16",
"property": "square",
"input": {
"square": 16
},
"expected": 32768
},
{
"description": "32",
"property": "square",
"input": {
"square": 32
},
"expected": 2147483648
},
{
"description": "64",
"property": "square",
"input": {
"square": 64
},
"expected": 9223372036854775808
},
{
"description": "square 0 raises an exception",
"property": "square",
"input": {
"square": 0
},
"expected": -1
},
{
"description": "negative square raises an exception",
"property": "square",
"input": {
"square": -1
},
"expected": -1
},
{
"description": "square greater than 64 raises an exception",
"property": "square",
"input": {
"square": 65
},
"expected": -1
}
]
},
{
"description": "returns the total number of grains on the board",
"property": "total",
"input": {},
"expected": 18446744073709551615
}
]
}
=end code``````
``````unit class Grains;

subset Square of Int where 1..64;

my @grains = 1, 2, 4, 8 ... Inf;

method square(Square \$x --> UInt) {
@grains[\$x - 1]
}

method total(-->UInt) {
@grains[^64].sum
}``````