C++ (calculating total number of grains)

Sep 14, 2015 at 3:25am
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.

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.
Sep 14, 2015 at 3:30am
No, thanks.
Sep 14, 2015 at 3:38am
I don't understand what you mean by "No, thanks"
Sep 14, 2015 at 3:50am
I'll give you a hint...

Think base two (2^0, 2^1, 2^2... 2^63).
Sep 14, 2015 at 5:33am
Ok, thanks
Sep 14, 2015 at 5:36am
i suggest using an array (array[8][8]). Then using a for loop(or a while loop cause you're a hipster) and just have an int that squares every time it loops.

1
2
3
4
5
6
7
8
9
10
11
12
13
int array[8][8];
int ricetotal = 1;

for (int i = 0; i < 8; i++)
{
    for (int j = 0; j < 8; j++)
{
    array[i][j] = ricetotal
    ricetotal = ricetotal * 2
}
}

std::cout << "it will take " << ricetotal << " grains of rice to do this";
Sep 14, 2015 at 5:42am
There's no need for an array. You know that there are 64 squares. The equation to find how much wheat a tile gives is quite simple: 2 ^ square_index. This equation assumes that you start your squares at an index of 0. Then simply loop from 0 to 63 and accumulate the value of all the squares.
Sep 14, 2015 at 5:47am
I was able to figure it out. The way Renthalkx97 suggested is much simpler.
Sep 14, 2015 at 11:04am

Hey, i think this program may help you.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;
int main()
{
int n,i,grains=0;
cout<<"enter the square number to find out the grains of rice at it";
cin>>n;
for(i=1;i<=n;i++)
{
  grains= grains+(grains*2);
}
cout<<"the number of grains is"<<grains;
return 0;
}
Last edited on Oct 3, 2015 at 4:30pm
Sep 14, 2015 at 11:36am
Simpliest way:
1
2
3
4
5
6
7
#include <iostream>
#include <limits>

int main()
{
    std::cout << std::numeric_limits<uint64_t>::max() << '\n';
}
Sep 14, 2015 at 8:56pm
I hope this helps....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "std_lib_facilities.h"

int main (){
    int square_no = 1;
    long long int grains = 0;
    for (grains = 1; grains <= 1000000000; grains *= 2)
         square_no++;

    cout << square_no << " number of squares are required to reach " << grains << '\n';
    return 0;



}
Sep 14, 2015 at 10:17pm
@MiiNiPaa

I believe you have to add up the values so 2 ^ 64 would be incorrect if so. It would be 2 ^ 0 + 2 ^ 1... + 2 ^ 63
Sep 15, 2015 at 12:59am
I believe you have to add up the values so 2 ^ 64 would be incorrect if so
std::numeric_limits<uint64_t>::max() is not 2^64.

Consider this:

   total
# squares | grains  |  equivalent
       1  |      1  |    2^1 - 1
       2  |      3  |    2^2 - 1
       3  |      7  |    2^3 - 1


Do we see a pattern there? I wonder what value std::numeric_limits<uint64_t>::max() is?
Last edited on Sep 15, 2015 at 1:00am
Sep 15, 2015 at 1:10am
Ahhh yea you're right because you have to take into account 0 which is what the -1 does.
Topic archived. No new replies allowed.