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:
Did you get the tests passing and the code clean? If you want to, these are some additional things you could try:
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?
Go through the setup instructions for PL/SQL to get ready to code:
Execute the tests by calling the
run method in the respective
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; /
JavaRanch Cattle Drive, exercise 6 http://www.javaranch.com/grains.jsp
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
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; /
create or replace package grains# is function at_square ( i_grain number ) return number; procedure total; end grains#; / create or replace package body grains# is function at_square ( i_grain number ) return number is l_grain number := 1; begin if i_grain = 1 then l_grain := i_grain; else for i in 2..i_grain loop l_grain := l_grain + l_grain ; end loop; end if; return l_grain; end at_square; procedure total is TYPE total_tab IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; l_total total_tab; l_tot number := 0; begin for i IN 1..64 loop l_total(i) := i; end loop; for i in 1..l_total.last loop l_tot := l_tot + grains#.at_square(i); end loop; dbms_output.put_line(l_tot); end total; end grains#; /
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.