# Using a while or do loop to represent the Rice and Chessboard case

• October 11th, 2012, 08:41 AM
WhyExist
Using a while or do loop to represent the Rice and Chessboard case
The rice and chessboard case = "Oh emperor, my wishes are simple. I only wish for this. Give me one grain of rice for the first square of the chessboard, two grains for the next square, four for the next, eight for the next and so on for all 64 squares, with each square having double the number of grains as the square before."

I have to use a while or do loop to replicate this but only upto the square that will contain a 100 grains of rice. (It is the 8th square where I hit 128 grains of rice.)

I have defined an integer as 1 and I have been trying to use *2 in the while loop but it only multiplies 1 by 2 an endless number of times.

I tried this:
int i = 1;
while (i<=64) {

int l = ((i)*2);
• October 11th, 2012, 09:15 AM
KevinWorkman
Re: Using a while or do loop to represent the Rice and Chessboard case
You should post your code in the form of an SSCCE so we can see what's really going on. Don't forget the highlight tags.

But you're going to have to think about how you solved it in the first place- how do you know that it's the 8th square where you should stop? What did you do in your head to figure that out? Write out the steps on paper, and that will be your algorithm that you have to turn into code.
• October 11th, 2012, 10:18 AM
jps
Re: Using a while or do loop to represent the Rice and Chessboard case
Quote:

while (i<=64) {
Quote:

only upto the square that will contain a 100 grains of rice
What does 64 have to do with a square containing 100 grains of rice?
• October 11th, 2012, 10:51 AM
WhyExist
Re: Using a while or do loop to represent the Rice and Chessboard case

I wrote the problem down on paper I can't see a correlation between the number of squares and the grains of rice. But I've noticed that the rice is multiplied by 2 each time.

Square 1 = 1 rice
Square 2 = 2 rice
Square 3 = 4 rice
Square 4 = 8 rice
Square 5 = 16 rice
Square 6 = 32 rice
Square 7 = 64 rice
Square 8 = 128 rice

I tried the following code:

public class ChessRice {
public static void main(String[] args) {
int maxSquares = 64;
int number;
number = 1;
for (int i=1; i<=maxSquares; i++) {
System.out.println((number*i)*2);
}
}
}

But it prints out:

2
4
6
8
10
12
14
16
etc
• October 11th, 2012, 10:52 AM
WhyExist
Re: Using a while or do loop to represent the Rice and Chessboard case
An error on my part it is was supposed to represent the amount of squares on a chessboard but I don't know why that was relevant.
• October 11th, 2012, 11:02 AM
KevinWorkman
Re: Using a while or do loop to represent the Rice and Chessboard case
Well, the only number you're incrementing is i. What does that represent? In the stuff you wrote out, where is the equivalent of i?
• October 11th, 2012, 11:27 AM
WhyExist
Re: Using a while or do loop to represent the Rice and Chessboard case
I think i is the equivalent of the grains of rice because for the next grain of rice on a square we are doing *2?
• October 11th, 2012, 11:29 AM
KevinWorkman
Re: Using a while or do loop to represent the Rice and Chessboard case
If i is equal to the grains of rice, and you're multiplying the grains of rice by 2 each time, what is the significance of adding 1 to i each time?
• October 11th, 2012, 11:29 AM
jps
Re: Using a while or do loop to represent the Rice and Chessboard case
If you want to loop until you have 100 grains of rice on a square, the conditional for the loop should include '100' and the 'number-of-grains-on-the-current-square' in question. This way if the requirements changed later on, you could adjust your code as easily as changing the 100 to a new value.
I also would like to mention that the 64 in the posted code is considered a magical number and should be replaced with a variable. Except that it should be a number other than 64 too. A variable like maxGrainsPerSquare or something with meaning...
• October 11th, 2012, 12:05 PM
WhyExist
Re: Using a while or do loop to represent the Rice and Chessboard case
I see. I must have read or misunderstood my textbook for some reason I thought i++ meant it displays the numbers in ascending order and i-- in descending order. What sorts of things can the last bit of for do?

for (int i = 1; i <= 100; i= i*2)

That is what I need but I just need to convert it to a while loop.

Thanks for the help.
• October 12th, 2012, 11:39 AM
KevinWorkman
Re: Using a while or do loop to represent the Rice and Chessboard case
Either way, I think you're trying to do too much inside the loop statement. Think about how you might use other variables instead of trying to do everything with a single variable i.