 ## to Grains in the PL/SQL Track

Published at Jul 13 2018 · 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.

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?

## Setup

Go through the setup instructions for PL/SQL to get ready to code:

http://exercism.io/languages/plsql

## Running the Tests

Execute the tests by calling the `run` method in the respective `ut_<exercise>#` package. The necessary code should be contained at the end of the test package. As an example, the test for the hamming exercise would be run using

``````begin
ut_hamming#.run;
end;
/
``````

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

### ut_grains#.plsql

``````create or replace package ut_grains#
is
procedure run;
end ut_grains#;
/

create or replace package body ut_grains#
is
procedure test (
i_descn                                       varchar2
,i_exp                                         number
,i_act                                         number
)
is
begin
if i_exp = i_act then
dbms_output.put_line('SUCCESS: ' || i_descn);
else
dbms_output.put_line('FAILURE: ' || i_descn || ' - expected ' || nvl('' || i_exp, 'null') || ', but received ' || nvl('' || i_act, 'null'));
end if;
end test;

procedure run
is
begin
test(i_descn => 'test_square_1'    , i_exp => 1                   , i_act => grains#.at_square(1 ));
test(i_descn => 'test_square_2'    , i_exp => 2                   , i_act => grains#.at_square(2 ));
test(i_descn => 'test_square_3'    , i_exp => 4                   , i_act => grains#.at_square(3 ));
test(i_descn => 'test_square_4'    , i_exp => 8                   , i_act => grains#.at_square(4 ));
test(i_descn => 'test_square_16'   , i_exp => 32768               , i_act => grains#.at_square(16));
test(i_descn => 'test_square_32'   , i_exp => 2147483648          , i_act => grains#.at_square(32));
test(i_descn => 'test_square_64'   , i_exp => 9223372036854775808 , i_act => grains#.at_square(64));
test(i_descn => 'test_total_grains', i_exp => 18446744073709551615, i_act => grains#.total        );
end run;
end ut_grains#;
/

begin
ut_grains#.run;
end;
/``````

### GRAINS#.pkb

``````CREATE OR REPLACE PACKAGE BODY PKZ.grains# AS
FUNCTION at_square(
num      NUMBER
)
RETURN   NUMBER
AS
BEGIN
RETURN POWER(2, num - 1);
END;

FUNCTION total
RETURN   NUMBER
AS
BEGIN
RETURN at_square(65) - 1;
END;
END grains#;
/``````

### GRAINS#.pks

``````CREATE OR REPLACE PACKAGE PKZ.grains# AS
FUNCTION at_square(
num      NUMBER
)
RETURN   NUMBER
;

FUNCTION total
RETURN   NUMBER
;

END grains#;
/``````