Published at Oct 14 2019
·
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?

Go through the setup instructions for Kotlin to install the necessary dependencies:

https://exercism.io/tracks/kotlin/installation

Execute the tests with:

```
$ gradlew test
```

Use

`gradlew.bat`

if you're on Windows

In the test suites all tests but the first have been skipped.

Once you get a test passing, you can enable the next one by removing the
`@Ignore`

annotation.

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.

```
import org.junit.Ignore
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import java.math.BigInteger
import kotlin.test.assertEquals
class BoardTest {
@Rule
@JvmField
var expectedException: ExpectedException = ExpectedException.none()
@Test
fun testSquare1ContainsCorrectNumberOfGrains() {
assertEquals(BigInteger.ONE, Board.getGrainCountForSquare(1))
}
@Ignore
@Test
fun testSquare2ContainsCorrectNumberOfGrains() {
assertEquals(BigInteger.valueOf(2), Board.getGrainCountForSquare(2))
}
@Ignore
@Test
fun testSquare3ContainsCorrectNumberOfGrains() {
assertEquals(BigInteger.valueOf(4), Board.getGrainCountForSquare(3))
}
@Ignore
@Test
fun testSquare4ContainsCorrectNumberOfGrains() {
assertEquals(BigInteger.valueOf(8), Board.getGrainCountForSquare(4))
}
@Ignore
@Test
fun testSquare16ContainsCorrectNumberOfGrains() {
assertEquals(BigInteger.valueOf(32768), Board.getGrainCountForSquare(16))
}
@Ignore
@Test
fun testSquare32ContainsCorrectNumberOfGrains() {
assertEquals(BigInteger.valueOf(2147483648), Board.getGrainCountForSquare(32))
}
@Ignore
@Test
fun testSquare64ContainsCorrectNumberOfGrains() {
assertEquals(BigInteger("9223372036854775808"), Board.getGrainCountForSquare(64))
}
@Ignore
@Test
fun testSquare0IsInvalid() {
expectedException.expect(IllegalArgumentException::class.java)
expectedException.expectMessage("Only integers between 1 and 64 (inclusive) are allowed")
Board.getGrainCountForSquare(0)
}
@Ignore
@Test
fun testNegativeSquareIsInvalid() {
expectedException.expect(IllegalArgumentException::class.java)
expectedException.expectMessage("Only integers between 1 and 64 (inclusive) are allowed")
Board.getGrainCountForSquare(-1)
}
@Ignore
@Test
fun testSquareGreaterThan64IsInvalid() {
expectedException.expect(IllegalArgumentException::class.java)
expectedException.expectMessage("Only integers between 1 and 64 (inclusive) are allowed")
Board.getGrainCountForSquare(65)
}
@Ignore
@Test
fun testBoardContainsCorrectNumberOfGrains() {
assertEquals(BigInteger("18446744073709551615"), Board.getTotalGrainCount())
}
}
```

```
import java.math.BigInteger
object Board {
fun getGrainCountForSquare(i: Int): BigInteger {
require((1..64).contains(i)) { "Only integers between 1 and 64 (inclusive) are allowed" }
return BigInteger("2").pow(i - 1)
}
fun getTotalGrainCount(): BigInteger {
var res = BigInteger("0")
(1..64).forEach { it ->
res += getGrainCountForSquare(it)
}
return res
}
}
```

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,092 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments