Concept explainers
Once upon a time in a kingdom far away, the king hoarded food and the people starved. His adviser recommended that the food stores be used to help the people, but the king refused. One day a small group of rebels attempted to kill the king but were stopped by the adviser. As a reward, the adviser was granted a gift by the king. The adviser asked for a few grains of wheat from the king’s stores to be distributed to the people. The number of grains was to be determined by placing them on a chessboard. On the first square of the chessboard, he placed one grain of wheat. He then placed two grains on the second square, four grains on the third square, eight grains on the fourth square, and so forth.
Compute the total number of grains of wheat that were placed on k squares by writing a recursive method getTotalGrains (k, grains). Each time getTotalGrains is called, it “places” grains on a single square; grains is the number of grains of wheat to place on that square. If k is 1, return grains. Otherwise, make a recursive call, where k is reduced by 1 and grains is doubled. The recursive call computes the total number of grains placed in the remaining k − 1 squares. To find the total number of grains for all k squares, add the result of the recursive call to grains and return that sum.
Want to see the full answer?
Check out a sample textbook solutionChapter 11 Solutions
Java Format: Unbound (saleable)
Additional Engineering Textbook Solutions
Starting Out With Visual Basic (8th Edition)
Digital Fundamentals (11th Edition)
Concepts Of Programming Languages
Starting Out with Java: Early Objects (6th Edition)
Web Development and Design Foundations with HTML5 (9th Edition) (What's New in Computer Science)
Objects First with Java: A Practical Introduction Using BlueJ (6th Edition)
- A quaker was walking down a country road when he was suddenly confronted by a robber brandishing a pistol. The robber exclaimed, "Your money or your life!" My friend replied, "I cannot deliver my money because I should be aiding thee in evildoing, but exchange is lawful, and I will give thee my purse for the pistol." The thief consented to accept the purse, then the quaker pointed the gun to the robber's head and said, "Now buddy, give me back my pocketbook or the weapon may go off fire," according to the robber.arrow_forwardThe game of chess was invented a few hundred years ago in India. The story has it, that the ruler of the area was so enchanted with the game, that he called the inventor to his palace, and asked him to name a gift. The seemingly humble man asked the ruler to put a grain of rice on the first square of the chessboard, two grains of rice on the second and so on, doubling the grains each time until all 64 squares of the chessboard were filled. The ruler was thinking about a full sack of rice and happily agreed. I didn't count it myself, but there are 32,000,000 grains of rice in a short ton (2,000 lbs). So do the calculation in Python and make a modern day comparison. Assume that a 50 foot rail car can carry 50 tons of rice. Write a program that would calculate how long the train would have be to carry the inventor's request? First Calculation: How many grain of rice? Second Calculation: How many tons of rice? Third Calculation: How many train car will be needed? BONUS(3pts) If your…arrow_forwardLegend has it that during the Jewish-Roman conflict, the first-century Jewish scholar Flavius Josephus was taken prisoner alongside a group of 40 fellow Jews. The captured troops made a decision to commit suicide rather than be caught and made plans to do so. To murder every third man until they were all dead, they were to surround the enemy in a circle. When Joseph and one other person decided they didn't want to participate, they rapidly estimated where they needed to be in the circle for survival. Create a programme that enables you to put n individuals in a circle and tell it to murder every m of them. The last individual in the group should be identified by number by the programme.arrow_forward
- A bandit with a weapon suddenly accosted a quaker as he walked along a remote road. "Your money or your life!" screamed the thief. Because I would be supporting thee in evildoing, my buddy said, "I cannot provide my money, but exchange is permissible, and I will offer thee my purse for the gun." He agreed to give it back, but he was threatened with gunfire, according to the thief, who claimed the quaker pulled a pistol at him and screamed, "Now buddy, give me back my wallet or I'll shoot you."arrow_forwardLegend has it that during the Jewish-Roman battle, the first-century Jewish historian Flavius Josephus was taken prisoner among a group of 40 other Jews. The captive troops made a decision to commit suicide rather than be caught and made plans to do so. To kill every third soldier until they were all dead, they were to surround the enemy in a circle. When Joseph and one other person decided they didn't want to participate, they rapidly calculated where they needed to be in the circle for survival. Create a software that enables you to put n persons in a circle and tell it to murder every m of them. The final individual in the circle should be identified by number by the software.arrow_forwardAn experiment with 10 participants was conducted to measure simple reaction time: the time to press a button in response to a visual stimulus (a light). This was a pilot study, so only five participants were tested. The reaction times in milliseconds for the five participants over 10 trials are shown below. Trial Susan Abdul Krisha Sam Sara T1 326 311 242 270 447 T2 395 256 184 492 429 T3 383 280 432 377 452 T4 337 344 454 315 556 T5 463 345 304 368 483 T6 194 287 385 302 307 T7 303 290 225 184 442 T8 235 266 425 483 319 T9 180 134 371 269 493 T10 484 265 293 312 375arrow_forward
- I was priviledged to have one of the 200 invitation cards printed for a wedding ceremony, when I reached the hotel I found two doors written 1 . bride relatives 2. Groom relatives I entered the one written groom relatives and found two more doors 1 . ladies 2. men I went through the one for men only to find two more doors 1 . people with gifts 2. people without gift I went through the one written people without gifts and found myself outside the hotel through the back door at the door it was written "So in this time of hard economy you want to just come, eat and drink without any gift, no way" Guests with gifts were warmly welcome Write an algorithm and flowchart for the expression above.arrow_forwardA pistol-wielding robber approached a Quaker on a rural road. "Your money or your life!" shouted the thief. "Exchange is allowed, and I will offer thee my purse for the weapon," my buddy said. The thief accepted the handbag, then the quaker held the pistol to his head and said, "Now friend, give me back my pocketbook or the weapon may go off fire," according to the robber.arrow_forwardFive members of an obesity club want to know how much they have lost or gained weight since the last time they met. To do this, they carry out a weighing ritual where each one is weighed on ten different scales in order to have a more accurate average of their weight. If there is a positive difference between this average weight and the weight of the last time they met, it means that they gained weight. But if the difference is negative, it means that they lost weight. What the problem requires is that for each person a sign is printed that says "went up" or "under" and the amount of pounds Write a program in c++ using for, do while or while. Initial weights 230, 240, 180, 300 and 350. Do not use arraysarrow_forward
- Five members of an obesity club want to know how much they have lost or gained weight since the last time they met. To do this, they carry out a weighing ritual where each one is weighed on ten different scales in order to have a more accurate average of their weight. If there is a positive difference between this average weight and the weight of the last time they met, it means that they gained weight. But if the difference is negative, it means that they lost weight. What the problem requires is that for each person a sign is printed that says "went up" or "under" and the amount of pounds. Write a program in C++ that uses a function. The user must enter the initial weight and the 10 weights of the scales. Please do not use arrays or vectorarrow_forwardFive members of an obesity club want to know how much they have lost or gained weight since the last time they met. To do this, they carry out a weighing ritual where each one is weighed on ten different scales in order to have a more accurate average of their weight. If there is a positive difference between this average weight and the weight of the last time they met, it means that they gained weight. But if the difference is negative, it means that they lost weight. What the problem requires is that for each person a sign is printed that says "went up" or "under" and the amount of pounds Write a program in c++ using a function. Initial weights 230, 240, 180, 300 and 350. Do not use arraysarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education