🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of The-F00L

The-F00L's solution

to Grains in the CFML Track

Published at Apr 13 2021 · 0 comments
Test suite

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.
  • 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?

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, cd into the exercise folder and run the following:

box task run TestRunner
# Or start up a test watcher that will rerun when files change
box task run TestRunner --:watcher

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:



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.


component extends="testbox.system.BaseSpec" {

	function beforeAll(){
	  SUT = createObject( 'Grains' );

	function run(){
		describe( "My Grains class", function(){			

			describe( 'returns the number of grains on the square', function(){

				it( '1', function(){
					expect( SUT.square( input='1' ) ).toBe( '1' );

				it( '2', function(){
					expect( SUT.square( input='2' ) ).toBe( '2' );

				it( '3', function(){
					expect( SUT.square( input='3' ) ).toBe( '4' );

				it( '4', function(){
					expect( SUT.square( input='4' ) ).toBe( '8' );

				it( '16', function(){
					expect( SUT.square( input='16' ) ).toBe( '32768' );

				it( '32', function(){
					expect( SUT.square( input='32' ) ).toBe( '2147483648' );

				it( '64', function(){
					expect( SUT.square( input='64' ) ).toBe( '9223372036854775808' );

				it( 'square 0 raises an exception', function(){
					expect( SUT.square( input='0' ) ).toBe( '-1' );

				it( 'negative square raises an exception', function(){
					expect( SUT.square( input='-1' ) ).toBe( '-1' );

				it( 'square greater than 64 raises an exception', function(){
					expect( SUT.square( input='65' ) ).toBe( '-1' );


			it( 'returns the total number of grains on the board', function(){
				expect( SUT.total() ).toBe( 18446744073709551615 );



component extends="GrainsTest" {

	function beforeAll(){
	  SUT = createObject( 'Solution' );

* Your implmentation of the Grains exercise
component {
	// in test case 64 fails with excepted value-1
	function square( input ) {
		if ((lsParseNumber(input)<1) || (lsParseNumber(input)>=65)) {
			return -1;
		return 2^(input-1);
	function total( input ) {
		return 2^64;

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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?