Calculate Grains of Wheat: A Program for Chess Inventor's Request

In summary, the code calculates the total number of grains of wheat on a square, checks if the total is at least 1000 grains, and outputs "It takes " + sqNum + " squares for at least 1000 grains."
  • #1
carl123
56
0
There is a story of the inventor of chess. The king wanted to reward him with riches, but he only asked for the following. One grain of wheat for the first square, two on the second, four on the third, doubling the amount for each square until 64 squares are accounted for.

Questions

1) Design a program for calculating the total number of grains of wheat. You will want to use iteration/looping. Do not forget to keep track of which square you are on and the number of grains of wheat that you currently have.

2) Write the program to determine the number of squares needed to get at least 1000, 1,000,000 and 1,000,000,000 grains of wheat.My code so far, works perfectly for number 1, Not sure how to go about number 2.

Code:
#include <iostream>
#include <cmath>
using namespace std;

int main() {
   int calc = 1;
   int sqNum = 1;
   unsigned long long total = 1;
   cout << "Square " << sqNum << " has " << total << " grain of wheat" << endl;
   for (int i = 1; i<64; ++i){
      sqNum++;
      calc = (calc * 2);
      total = total + calc;
      cout << "Square " << sqNum << " has " << total << " grains of wheat " << endl;
       
   }
   
   return 0;
}
 
Technology news on Phys.org
  • #2
A couple of comments about what you have so far:

You don't need the variable [m]sqNum[/m] since it always has the same value as your looping index. I would try to incorporate all squares (including the first) into the loop so the first square doesn't have to be handled separately before the loop.

In order to do part 2.), it seems you need a conditional to check if [m]total[/m] is at least as large as some given value, and then a means to exit from the loop if it is.

Or, you could instead use a while loop instead of a for loop (as it allows defining a condition for continuing), which is what I think I would do in this case. :)
 
  • #3
Sorry, I'm not sure i understood what you meant. Can you explain further?
 
  • #4
carl123 said:
Sorry, I'm not sure i understood what you meant. Can you explain further?

Which part was unclear?
 
  • #5
the part where i have to use a conditional to check if "total" is at least as large as some given value
 
  • #6
carl123 said:
the part where i have to use a conditional to check if "total" is at least as large as some given value

Well, for example, if I was going to do this in PHP, and I wanted to determine the number of squares to get at least 1,000 grains, I might write:

PHP:
$total = 0;
$sqr = 0
while ($total < 1000)
{
	$total = pow(2, ++$sqr) - 1;
}
echo "It takes at least $sqr squares to obtain at least 1,000 grains."

Note: I have relied in my code on the fact that:

\(\displaystyle \sum_{k=0}^{n}\left(2^k\right)=2^{n+1}-1\)
 
  • #7
Here's another way to do your problem. First, the problem said you should find the total number of grains. Your program outputs the number of grains on each square for a total of 64 lines of output. The specifications say that there are only 4 lines of output. Accordingly, I moved your cout outside your for loop. Next, introduce another variable oldTotal, which is the value of total the previous iteration of the for loop. You can add two more if statements in the for loop to answer the questions about 1000000 and 1000000000000 grains.
Code:
int main() {
   int calc = 1;
   int sqNum = 1;
   unsigned long long total = 1,oldTotal;
   cout << "Square " << sqNum << " has " << total << " grain of wheat" << endl;
   for (int i = 1; i<64; ++i){
      sqNum++;
      calc = (calc * 2);
      oldTotal = total;
      total = total + calc;
      if (oldTotal < 1000 && total >= 1000) {
         cout<<"It takes " << sqNum << " squares for at least 1000 grains." << endl;
      }
      \\ you add two more if statements
   }
      cout << "Square " << sqNum << " has " << total << " grains of wheat " << endl;   
   return 0;
}
 

What is the purpose of the "Calculate Grains of Wheat" program?

The purpose of the program is to fulfill the request of a chess inventor to calculate the number of grains of wheat needed to cover a chessboard, with the first square having 1 grain, the second square having 2 grains, and so on, doubling the number of grains for each subsequent square.

What is the mathematical formula used in the program?

The formula used is 2n-1, where n represents the number of the square on the chessboard.

What is the practical application of this program?

This program can be used to estimate the number of grains needed for a chessboard, which can then be used to plan for the materials and resources needed for a chess set production or to understand the magnitude of the inventor's request.

What is the maximum number of squares that can be calculated in this program?

The maximum number of squares that can be calculated is 64, as there are 64 squares on a standard chessboard.

How long does it take for the program to calculate the number of grains for all 64 squares?

The program should be able to calculate the number of grains for all 64 squares almost instantly, as it is a simple mathematical operation for the computer.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
3
Views
734
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
30
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top