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

Click For Summary

Discussion Overview

The discussion revolves around a programming challenge based on a mathematical problem related to the doubling of grains of wheat on a chessboard. Participants are tasked with designing a program to calculate the total number of grains of wheat on each square and to determine how many squares are needed to reach specified grain counts (1000, 1,000,000, and 1,000,000,000). The scope includes programming techniques, particularly iteration and conditionals.

Discussion Character

  • Technical explanation
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant presents an initial program that calculates the total number of grains of wheat on each square of the chessboard, using a for loop.
  • Another participant suggests removing the variable for the square number since it can be derived from the loop index and recommends incorporating all squares into the loop.
  • There is a discussion about using conditionals to check if the total number of grains meets specified thresholds, with suggestions to use a while loop for this purpose.
  • Some participants express confusion about the implementation of conditionals and request further clarification on how to check if the total is at least a given value.
  • A participant provides a PHP example to illustrate how to determine the number of squares needed to reach a specific grain count, referencing a mathematical formula for the sum of powers of two.
  • Another participant proposes a modification to the original code to reduce output to four lines, suggesting the introduction of an additional variable to track the previous total and adding conditionals to check for the specified grain counts within the loop.

Areas of Agreement / Disagreement

Participants generally agree on the need to modify the original program to meet the specifications, but there is no consensus on the best approach to implement the conditionals or the structure of the loop. Some participants express confusion about the suggested changes, indicating that the discussion remains unresolved in terms of clarity and implementation details.

Contextual Notes

Limitations include potential misunderstandings of programming concepts such as conditionals and loop structures, as well as the need for clarity in the problem requirements regarding output format.

carl123
Messages
55
Reaction score
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
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. :)
 
Sorry, I'm not sure i understood what you meant. Can you explain further?
 
carl123 said:
Sorry, I'm not sure i understood what you meant. Can you explain further?

Which part was unclear?
 
the part where i have to use a conditional to check if "total" is at least as large as some given value
 
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:

$$\sum_{k=0}^{n}\left(2^k\right)=2^{n+1}-1$$
 
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;
}
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
12
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
12K
  • · Replies 1 ·
Replies
1
Views
3K