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: An Introduction to Problem Solving and Programming (8th Edition)
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)
- Legend 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_forwardA computer science professor has bought his two children a box of raisin cookies for thanksgiving. The number of raisins in the cookies is not evenly distributed and each cookie can have as many raisins in it. The children love raisins very much and when they open the box they fight over having as many raisins possible. They do not care about the number of cookies they have but rather the total number of raisins they collected. They are not happy with what they have collected, because each of them thinks the other one has more raisins. So they come back to their father for help. The professor comes up with a dynamic programming algorithm to find whether it is possible to, without breaking any cookie, assign all of them to his children where each gets an equal number of raisins. As mentioned it is not important if one of them gets more cookies. The total number of raisins is T and the total number of cookies is n. Write down a dynamic programming algorithm with O(nT) time complexity…arrow_forwarda manuscript page generally has about 500 words on it. if you read a manuscript in 1 minute and 24 seconds, what is your reading rate in words per minute? what is the number of pages you read per hour?arrow_forward
- Legend 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_forwardBlue-Eyed Island: A group of individuals live on an island until a visitor arrives with an unusual order: all blue-eyed people must leave the island as quickly as possible. Every evening at 8:00 p.m., a flight will depart. Everyone can see everyone else's eye colour, but no one knows their own (nor is anyone allowed to tell them). Furthermore, they have no idea how many people have blue eyes, but they do know that at least one person does. How long will it take for the blue-eyed individuals to leave?arrow_forwardSkydiving Bibi is a skydiving instructor. Before carrying out skydiving activities, Bibi will always ask the name, height, and age of the participants. Bibi's participants is always 2 people. To make it easier to remember their identities, Bibi gave them a paper to write down their identity. But because they are very enthusiastic in doing skydiving, they write their identity in a single line only (for both of the participants), so it wasn't quite neat and it made difficult for Bibi to see it, and resulted in the skydiving process being disturbed. As one of the organizer committee, you do not want this activity to be disturbed and plan to provide a program that provides their identity neatly to Bibi. Format Input Input consists of 1 line. It contains the identity of 2 people in a row Ni, Ti, Ui - the name, height, and age of the i-th participant. Format Output The output consists of 6 lines. The first line, "Name 1: N1". The second line, "Height 1: T1". The third line, "Age: U1". For…arrow_forward
- Welcome to your Tape One day Jojo received a mysterious box which contains cassette tapes in front of his doorstep, it turns out that the tapes contain a voice recording made by someone and on each tape the narrator is talking about a different person. Jojo didn’t understand at all the purpose of these tapes but when he told about them to Lili she told him that she could think up to thirteen reasons why those recordings were being made. Anyway, Jojo decides to sort the tapes he found according to the alphabetical order of the names of the people being talked about on the tapes. Now help Jojo to tell whose tape is on the K-th order after he has done the sorting! Notes: The order is started from number 1. Format Input The first line contains a single number T, the number of testcases. Each testcase contains the number N, which is the number of tapes, and the number K. The next N lines contain Ai which is the name of the person being talked about on the i-th tape (before sorting). Format…arrow_forward. According to legend, the first century Jewish historian, Flavius Josephus,was captured along with a band of 40 compatriots by Roman soldiers duringthe Jewish–Roman war. The captured soldiers decided that they preferredsuicide to being captured and devised a plan for their demise. They wereto form a circle and kill every third soldier until they were all dead. Josephand one other decided they wanted no part of this and quickly calculatedwhere they needed to place themselves in the circle so that they would bothsurvive. Write a program that allows you to place n people in a circle andspecify that every m person will be killed. The program should determinethe number of the last person left in the circle. Use a circularly linked listto solve the problemarrow_forwardQ:With the advancement in technology, computer related crimes are taking birth rapidly. One day, a hacker stole the data from one of the computers being used in the bank and transferred confidential information to the opponents. After detectors found the criminal, all became surprised as he was no other but the assistant manager of the same bank. Keeping the scenario in mind, enlist all the reasons behind this immoral act and propose some suggestions to overcome such acts.arrow_forward
- Melissa has invested a huge amount of money into the stock market. She doesn’t trust just anyoneto give her advice regarding the buying and selling information. Normally, before she will buy acertain stock, she must get input from three sources. The first source is Adam, a famousstockbroker. The second source is Mary, a self-made millionaire in the stock market, and the thirdsource is Laura, the world-famous psychic. After several months of receiving advice from all threesources, she has come to the following conclusions:I. Buy the stock if Adam and Mary both say yes and Laura says no.II. Buy the stock if Laura and Adam say yes and Mary says no.III. Buy the stock if Laura says yes and Adam or Mary say no.IV. All three sources say yes.V. Don’t buy otherwise.Use the following:A: Advice from Adam (0 for not buy, 1 for buy)B: Advice from Mary (0 for not buy, 1 for buy)C: Advice from Laura (0 for not buy, 1 for buy)X: Melissa decision (0 for not buy, 1 for buy) i. Construct a truth table to…arrow_forwardDoctor Hendra, the only dentist in town, has a computer system that can help him make appointments for patients and dental treatment for patients. Doctor Hendra Receptionist will make appointments with patients when they call or when they are back in the waiting room after treatment. Sometimes patients also call to cancel appointments or change them. Doctor Hendra keeps a note on the system about his patient treatment which is updated every time he sees a patient. Then Doctor Hendra Receptionist also made a payment bill for the patient and made a payment record in the system. Make a use case diagram of this case!arrow_forwardNorah lives with her grandfather in a small village. She is busy teaching her and her grandfather is busy taking care of his farm that includes 50 palm trees. Grandfather got sick one day and could no longer take care of the farm as before. Norah wanted to help her grandfather and take care of the farm for him, but she didn't know how to do it. Norah noticed the discoloration of some palm trees and did not know why or how to deal with the problem. She tried to communicate with the palm cultivation experts but to no avail. Norah did not know about the palm seasons, such as the pollination season and other seasons. She also does not know where to order palm care materials. Norah thought and asked herself: Are there other people in Saudi Arabia, a country of 30 million palm trees, who need help like me? Norah applied to the Ministry of Environment, Water and Agriculture to find a solution for her and for people who are suffering like her. The Ministry of Environment, Water and Agriculture…arrow_forward
- Operations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks Cole