473,396 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Rice grains on a chessboard

Hey guys I'm a beginner with C++ and am doing a practice problem for my class and am having trouble figuring this problem out.

A man wants 1 grain for the 1st square on a chessboard. 2 grains on the 2nd. 4 grains on the 3rd. 8 grains for the 4th square. etc etc (doubling for each square)

I have to write a program that will tell me how many squares it would take to reach at least 1,000 grains of rice, 1,000,000 grains of rice, and 1,000,000,000 grains of rice.

Every thing has to be in INT's not doubles. If anyone can help me out it would be greatly appreciated. I'm completely lost on how to do this. Thanks!
Oct 2 '09 #1
8 8374
Banfa
9,065 Expert Mod 8TB
Have you written anything yet.

How about a program that prints out the number of grains of on each square for the first 31 squares? You can work on the limits later.

If you can't do a program like that then try using pen and paper to write down the number of grains of rice on the first 4 or 5 squares. Once you have the method for doing that translate it into a programable algorithm.
Oct 3 '09 #2
donbock
2,426 Expert 2GB
@vmanrao
You can't count the number of grains of rice with an INT -- the largest number guaranteed to fit in an unsigned INT on all compilers is 65535. You'll have to find a tricky way to solve the problem.
Oct 3 '09 #3
Banfa
9,065 Expert Mod 8TB
Yes but if you take INT to mean integer arithmetic and take into account what's been asked to be calculated then you will notice that it does fit inside 32 bit integer arithmetic.
Oct 3 '09 #4
whodgson
542 512MB
On my machine declaring only ints, employing a for loop with the condition set to i<31 the squares hold the following number of rice grains:
boardsq 0 = 1
boardsq 1 = 2
boardsq 2 = 4
boardsq 3 = 8
boardsq 4 = 16
boardsq 5 = 32
boardsq 6 = 64
boardsq 7 = 128
boardsq 8 = 256
boardsq 9 = 512
boardsq 10 = 1024 <------
boardsq 11 = 2048
boardsq 12 = 4096
boardsq 13 = 8192
boardsq 14 = 16384
boardsq 15 = 32768
boardsq 16 = 65536
boardsq 17 = 131072
boardsq 18 = 262144
boardsq 19 = 524288
boardsq 20 = 1048576 <-------
boardsq 21 = 2097152
boardsq 22 = 4194304
boardsq 23 = 8388608
boardsq 24 = 16777216
boardsq 25 = 33554432
boardsq 26 = 67108864
boardsq 27 = 134217728
boardsq 28 = 268435456
boardsq 29 = 536870912
boardsq 30 = 1073741824 <-------
boardsq 31 = -2147483648
At square 31 the result returns a negative which is still valid if multiplied by -1
After square 31 the result returned is zero (0) ..... but in this case the requirements are met. Someone might like to suggest the pseudo code which will get to square no 64.
Oct 5 '09 #5
donbock
2,426 Expert 2GB
@vmanrao
Do you need to find the first square to hold that many grains of rice; or do you need to find the minimum number of squares such that the sum of all grains of rice is at least that number?
Oct 5 '09 #6
donbock
2,426 Expert 2GB
@whodgson
Do these values look strangely familiar? There is an elegant and very rapid algorithm lurking behind the pattern made by these values.
Oct 5 '09 #7
newb16
687 512MB
But in the base (namely , 2) where these numbers look familiar, numbers like 1000, 1000000 look unfamiliar and will require effort to implement arithmetic operation with multi-word precision.
Oct 5 '09 #8
Banfa
9,065 Expert Mod 8TB
No because one of the beauties of C and C++ is the ability to use which ever base, 8, 10, 16(and by extension 2) is convenient at the time so you can use base 16(2) operations for calculation but base 10 operations for comparison.
Oct 5 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Michel | last post by:
I have an html page which, thanks to some javascript, allows to move pieces on a chessboard. I can't find how to reflect the moves in a list displayed by the same page. To understand what I'm...
3
by: Tony Johansson | last post by:
Hello! Assume you want to store field object that a chess board consist of. A chess board consist of 64 fields where each field is either white or black. Now to my question how should I...
20
by: Chad Everett | last post by:
Hi all, I am new to the group. Trying to learn Python programming on my own. I am working through Michael Dawson's Book Python Programming for the absolute beginner. I am tring to write a...
17
by: Jonathan Pritchard | last post by:
I know this is a simple problem, but I've just included this in the title because it explains what my program tries to do. The following does not work, it for someone reason does not want to...
6
by: tommy34 | last post by:
I'm new to java and any help will be appreciated. Problem is i dont know even where to start. I need to write a program that reads a chessboard configuration and identifies whether a king is...
5
by: ebrimagillen | last post by:
Hello mates, Just needed a solution on the exercises below. Exercise 2 A classic problem in introductory programming is the grains of rice on a chess board problem. Your program should...
4
by: punkybrewster | last post by:
I have the idea to create the program but I need help in finishing it. My task is to write a program that prints out a chessboard using only asterisks. I have to use multiple nested loops with...
1
by: satish100 | last post by:
how can i write a program for making the plain chessboard in PHP
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.