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 (7th 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)
- Suppose you always eat a particular brand of cookies out of the standard-sized package it comes in from the store, which has 4 rows of cookies in equal amounts. You notice that when you eat 3 at a time, there's always one cookie left over. you decide when you purchase the next package, you'll eat 5 at a time but still, there's always one cookie left over. Since it really isn't helpful to always have a leftover cookie you decide to eat 7 at a time and there are no cookies leftover, but you do notice that in your last grab of 7 cookies, you're one cookie shortarrow_forwardA bandit with a gun approached a quaker as he strolled along a remote road. The thief made an ultimatum: "Your money or your life!" My buddy said, "I can't give you my money because then I'd be helping you do evil, but exchange is legal, so here, take my pocketbook in return for your gun." After the robber agreed to take the handbag, the Quaker put the pistol to his head and demanded the purse back, with the ominous warning, "Now friend, give me back my pocketbook or the weapon may go off fire."arrow_forwardAs you visit the La Filipina Ecopark together with your friend, you can't help but admire the nature around you including the many species of our native trees. Despite your interest, you are a very unskilled botanist and have a lot of trouble identifying different trees. Your botanist friend gives you partial guidance and you decide to write a program that will give you the name of the tree based on its characteristics. Your botanist told you that there are only 5 tree species in this eco-park, he gave you a list of 5 tree species: Tree Species Dao Katmon Yakal Kamagong Gisok-gisok Wood Density Tree size 0.500 to 0.600 0.601 to 0.700 0.701 and above 0.701 and above 0.701 and above L M L M S Assume that you can instantly measure the wood density and infer its tree size correctly. Your program should read the actual measurement of wood density (i.e. double) and your inferred tree size (i.e. char) and should be able to determine and display the name of the corresponding tree. If the wood…arrow_forward
- Part 6: Boolean Logic Expressions Boolean expressions are types of logical operations that we can perform on true and false values. Note that the word Boolean is always capitalized because it was named after its inventor, George Boole. Boolean algebra is a very important topic in computer science, and if you haven't learned it before you definitely will in the future. However, for our purposes we are only interested in how we can use them to manipulate binary numbers. The way we use them on binary numbers is by treating 1 as true and 0 as false. From this point on I will be using 0 and 1 instead of false and true. There are many types of Boolean expressions, but the three most important ones are AND, OR, and NOT. AND takes 2 operands and will output 1 if they are both 1, or 0 otherwise OR takes 2 operands and will output 1 if either one is 1, or 0 if neither is 1 NOT takes 1 operand and reverses it: 1 becomes 0, and 0 becomes 1 We can represent this behavior using something called a…arrow_forwardpython programming Write a program using Matplotlib that will ask the user to create a pie chart with some values. Then ask the user for how many inputs they wish to have to determine how many slices will be present in the pie Further, ask the user for the colors of the pie they wish to use and the weighting size for each piece to create the pie chart diagram. Lastly ask the user which piece of the pie they want exploded from the pie chart and return the final image to the user. Save the image and upload it with your solution with a sample run.arrow_forwardStory: Once upon a time a farmer went to a market and purchased a wolf, a goat, and a cabbage. On his way home, the farmer came to the bank of a river and rented a boat. But crossing the river by boat, the farmer could carry only himself and a single one of his purchases: the wolf, the goat, or the cabbage. If left unattended together, the wolf would eat the goat, or the goat would eat the cabbage. Problem: Create a Flowchart and Algorithm of all the possible solutions on how the farmer carry himself and his purchases to the far bank of the river, leaving each purchase intact.arrow_forward
- Virus DNA files As a future doctor, Jojo works in a laboratory that analyzes viral DNA. Due to the pandemic During the virus outbreak, Jojo received many requests to analyze whether there was any viral DNA in the patient. This number of requests made Jojo's job even more difficult. Therefore, Jojo ask Lili who is a programmer for help to make a program that can read the file containing the patient's DNA data and viral DNA and then match them. If on the patient's DNA found the exact same string pattern, then write to the index screen the found DNA. Data contained in the file testdata.in Input Format The first line of input is the number of test cases TThe second row and so on as many as T rows are the S1 string of patient DNA and the S2 string of viral DNA separated by spaces Output Format The array index found the same string pattern. Constraints 1 ≤ T ≤ 1003 ≤ |S2| ≤ |S1| ≤ 100|S| is the length of the string.S will only consist of lowercase letters [a-z] Sample Input (testdata.in)…arrow_forwardOne hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem. Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight. Input The first (and the only) input line contains integer number w (1 s w s 100) – the weight of the watermelon bought by the boys. Output Print YES, if the boys can divide the watermelon into two parts, each of them…arrow_forwardA step-by-step procedure used to solve a problem is called Algorithm why?arrow_forward
- A step-by-step procedure used to solve a problem is called Algorithm explain?arrow_forwardLab #5 - The Psychic Computer NetworkOne stormy night, as Dionne worked diligently on her Computer Programming labs, a bolt of lightning struck the house. The power surge raced through her computer, causing it to emit a strange glow. A strange new program appeared on her computer called "psychic". Dionne told all her friends that this program was capable of predicting the future. If they wanted to use the program, for a small fee, they could download it from her web site.Objective - To write a program that will predict the future Method - 1. Use the main procedure toa. Prompt the user for his/her lucky number. Use this number as input to the srand function found in cstdlibb. Prompt the user for which life topic he wishes his predictions based. Based on the user's response call one of three functions. Suggestions are love, finance, and school, but you may use three of your own choosing. Use a nested if-else statement to control which function is called.2. Each function willc. Call the…arrow_forwardThe total nuclear binding energy is the energy required to split a nucleus of an atom in its component parts: protons and neutrons, or, collectively, the nucleons. It describes how strongly nucleons are bound to each other. When a high amount of energy is needed to separate the nucleons, it means nucleus is very stable and the neutrons and protons are tightly bound to each other. The atomic number or proton number (symbol Z) is the number of protons found in the nucleus of an atom. The sum of the atomic number Z and the number of neutrons N gives the mass number A of an atom. Figure 1: Binding Energy in the Nucleus The approximate nuclear binding energy Eb in million electron volts, of an atomic nucleus with atomic number Z and mass number A is calculated using the following formula: See pictures The binding energy per nucleon (BEN) is calculated by dividing the binding energy (Eb) by the mass number (A). You are asked to write a program that requests the user for a valid atomic…arrow_forward
- Operations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks ColeProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
- A+ Guide To It Technical SupportComputer ScienceISBN:9780357108291Author:ANDREWS, Jean.Publisher:Cengage,