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# AS total constant NUMBER := 18446744073709551615; FUNCTION at_square(p_square NUMBER) RETURN NUMBER; END grains#; / CREATE OR REPLACE PACKAGE BODY grains# AS FUNCTION at_square(p_square NUMBER) RETURN NUMBER AS v_res NUMBER := 1; BEGIN IF p_square = 1 THEN RETURN 1; END IF; FOR i in 2..p_square LOOP v_res := v_res * 2; IF v_res > total THEN RAISE_APPLICATION_ERROR (-20001, 'Input too high. Try a number up to 64.'); END IF; END LOOP; RETURN v_res; END at_square; 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.