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

Published at Jan 27 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, with the number of grains doubling on each successive square.

There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).

Write code that shows:

- how many grains were on a given square, and
- the total number of grains on the chessboard

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?

In order to run the tests for this track, you will need to install DUnitX. Please see the installation instructions for more information.

If Delphi is properly installed, and `*.dpr`

file types have been associated with Delphi, then double clicking the supplied `*.dpr`

file will start Delphi and load the exercise/project. `control + F9`

is the keyboard shortcut to compile the project or pressing `F9`

will compile and run the project.

Alternatively you may opt to start Delphi and load your project via. the `File`

drop down menu.

We monitor the Pascal-Delphi support room on gitter.im to help you with any questions that might arise.

Note that, when trying to submit an exercise, make sure the exercise file you're submitting is in the `exercism/delphi/<exerciseName>`

directory.

For example, if you're submitting `ubob.pas`

for the Bob exercise, the submit command would be something like `exercism submit <path_to_exercism_dir>/delphi/bob/ubob.pas`

.

JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp

It's possible to submit an incomplete solution so you may request help from a mentor.

```
unit uGrainsTests;
interface
uses
DUnitX.TestFramework;
const
CanonicalVersion = '1.1.0';
type
[TestFixture]
TgrainsTests = class(TObject)
public
[Test]
// [Ignore('Comment the "[Ignore]" statement to run the test')]
procedure Test_square_1;
[Test]
[Ignore]
procedure Test_square_2;
[Test]
[Ignore]
procedure Test_square_3;
[Test]
[Ignore]
procedure Test_square_4;
[Test]
[Ignore]
procedure Test_square_16;
[Test]
[Ignore]
procedure Test_square_32;
[Test]
[Ignore]
procedure Test_square_64;
[Test]
[Ignore]
procedure Square_0_raises_an_exception;
[Test]
[Ignore]
procedure Negative_square_raises_an_exception;
[Test]
[Ignore]
procedure Square_greater_than_64_raises_an_exception;
[Test]
[Ignore]
procedure Returns_the_total_number_of_grains_on_the_board;
end;
implementation
uses System.SysUtils, uGrains;
procedure TgrainsTests.Test_square_1;
var expected: UInt64;
begin
expected := 1;
Assert.AreEqual(expected, Grains.Square(1));
end;
procedure TgrainsTests.Test_square_2;
var expected: UInt64;
begin
expected := 2;
Assert.AreEqual(expected, Grains.Square(2));
end;
procedure TgrainsTests.Test_square_3;
var expected: UInt64;
begin
expected := 4;
Assert.AreEqual(expected, Grains.Square(3));
end;
procedure TgrainsTests.Test_square_4;
var expected: UInt64;
begin
expected := 8;
Assert.AreEqual(expected, Grains.Square(4));
end;
procedure TgrainsTests.Test_square_16;
var expected: UInt64;
begin
expected := 32768;
Assert.AreEqual(expected, Grains.Square(16));
end;
procedure TgrainsTests.Test_square_32;
var expected: UInt64;
begin
expected := 2147483648;
Assert.AreEqual(expected, Grains.Square(32));
end;
procedure TgrainsTests.Test_square_64;
var expected: UInt64;
begin
expected := 9223372036854775808;
Assert.AreEqual(expected, Grains.Square(64));
end;
procedure TgrainsTests.Square_0_raises_an_exception;
var MyProc: TTestLocalMethod;
begin
MyProc := procedure
begin
Grains.Square(0);
end;
Assert.WillRaise(MyProc, ERangeError);
end;
procedure TgrainsTests.Negative_square_raises_an_exception;
var MyProc: TTestLocalMethod;
begin
MyProc := procedure
begin
Grains.Square(-1);
end;
Assert.WillRaise(MyProc, ERangeError);
end;
procedure TgrainsTests.Square_greater_than_64_raises_an_exception;
var MyProc: TTestLocalMethod;
begin
MyProc := procedure
begin
Grains.Square(65);
end;
Assert.WillRaise(MyProc, ERangeError);
end;
procedure TgrainsTests.Returns_the_total_number_of_grains_on_the_board;
var expected: UInt64;
begin
expected := 18446744073709551615;
Assert.AreEqual(expected, Grains.Total);
end;
initialization
TDUnitX.RegisterTestFixture(TgrainsTests);
end.
```

```
unit uGrains;
interface
uses
System.SysUtils;
type
Grains = class
class function Square(ANumber: integer): UInt64;
class function Total: UInt64;
end;
implementation
uses
System.Math;
var
count: UInt64;
{ TGrains }
class function Grains.Square(ANumber: integer): UInt64;
var
I: Integer;
soma: UInt64;
begin
if (ANumber <= 0) or (ANumber > 64) then
raise ERangeError.Create('Number out of range!');
if (ANumber = 1) then
Result := 1
else
Result := Square(ANumber - 1) * 2;
end;
class function Grains.Total: UInt64;
Var
i :Integer;
begin
Result:= 0;
for I := 1 to 64 do
Result := Result + Square(i);
end;
end.
```

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?

Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments