Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
11th Edition
ISBN: 9780134671710
Author: Y. Daniel Liang
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Textbook Question
Chapter 18, Problem 18.3PE
(Compute greatest common divisor using recursion) The gcd (m, n) can also be defined recursively as follows:
- If m % n is 0, gcd(m, n) is n.
- Otherwise, gcd (m, n) is gcd (n, m % n).
Write a recursive method to find the GCD. Write a test
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Java Program: Recursive Method
There are n people in a room where n is an integer greater then or equal to 2. Each person shakes hands once with every other person. What is the total number of handshakes in the room? Write a recursive method to solve this problem with the following header:public static int handshake(int n)where handshake(n) returns the total number of handshakes for n people in the room. To get you started if there are only one or two people in the room, then:handshake(1)=0handshake(2)=1
1. Write a recursive method expFive(n) to compute y=5^n. For instance, if n is 0, y is 1. If n is 3,
then y is 125. If n is 4, then y is 625. The recursive method cannot have loops. Then write a
testing program to call the recursive method. If you run your program, the results should look like
this:
> run RecExpTest
Enter a number:
3
125
>run RecExpTest
Enter a number:
3125
2. For two integers m and n, their GCD(Greatest Common Divisor) can be computed by a
recursive function. Write a recursive method gcd(m,n) to find their Greatest Common
Divisor. Once m is 0, the function returns n. Once n is 0, the function returns m. If neither
is 0, the function can recursively calculate the Greatest Common Divisor with two
smaller parameters: One is n, the second one is m mod n. Although there are other
approaches to calculate Greatest Common Divisor, please follow the instructions in
this question, otherwise you will not get the credit. Meaning your code needs to follow
the given algorithm.
Then…
Fibonacci numbers are a sequence of integers, starting with 1, where the value of each number is the sum of the two previous numbers, e.g. 1, 1, 2, 3, 5, 8, etc. Write a function called fibonacci that takes a parameter, n, which contains an integer value, and have it return the nth Fibonacci number. (There are two ways to do this: one with recursion, and one without.)
Chapter 18 Solutions
Introduction to Java Programming and Data Structures: Brief Version (11th Global Edition)
Ch. 18.2 - What is a recursive method? What is an infinite...Ch. 18.2 - Prob. 18.2.2CPCh. 18.2 - Show the output of the following programs and...Ch. 18.2 - Prob. 18.2.4CPCh. 18.2 - Prob. 18.2.5CPCh. 18.2 - Write a recursive mathematical definition for...Ch. 18.3 - Prob. 18.3.1CPCh. 18.3 - What is wrong in the following methods?Ch. 18.3 - Prob. 18.3.3CPCh. 18.4 - Describe the characteristics of recursive methods.
Ch. 18.4 - Prob. 18.4.2CPCh. 18.4 - Prob. 18.4.3CPCh. 18.5 - Prob. 18.5.1CPCh. 18.5 - Prob. 18.5.2CPCh. 18.5 - What is a recursive helper method?Ch. 18.6 - Prob. 18.6.1CPCh. 18.6 - How does the program get all files and directories...Ch. 18.6 - How many times will the getSize method be invoked...Ch. 18.6 - Will the program work if the directory is empty...Ch. 18.6 - Will the program work if line 20 is replaced by...Ch. 18.6 - Will the program work if lines 20 and 21 are...Ch. 18.7 - Prob. 18.7.1CPCh. 18.8 - Prob. 18.8.1CPCh. 18.8 - Prob. 18.8.2CPCh. 18.8 - How many times is the displayTriangles method...Ch. 18.8 - Prob. 18.8.4CPCh. 18.8 - Prob. 18.8.5CPCh. 18.9 - Which of the following statements are true? a. Any...Ch. 18.9 - Prob. 18.9.2CPCh. 18.10 - Identify tail-recursive methods in this chapter.Ch. 18.10 - Rewrite the fib method in Listing 18.2 using tail...Ch. 18 - Prob. 18.1PECh. 18 - Prob. 18.2PECh. 18 - (Compute greatest common divisor using recursion)...Ch. 18 - (Sum series) Write a recursive method to compute...Ch. 18 - (Sum series) Write a recursive method to compute...Ch. 18 - (Sum series) Write a recursive method to compute...Ch. 18 - (Fibonacci series) Modify Listing 18.2,...Ch. 18 - Prob. 18.8PECh. 18 - (Print the characters in a string reversely) Write...Ch. 18 - (Occurrences of a specified character in a string)...Ch. 18 - Prob. 18.11PECh. 18 - (Print the characters in a string reversely)...Ch. 18 - (Find the largest number in an array) Write a...Ch. 18 - (Find the number of uppercase letters in a string)...Ch. 18 - Prob. 18.15PECh. 18 - (Find the number of uppercase letters in an array)...Ch. 18 - (Occurrences of a specified character in an array)...Ch. 18 - (Tower of Hanoi) Modify Listing 18.8,...Ch. 18 - Prob. 18.19PECh. 18 - (Display circles) Write a Java program that...Ch. 18 - (Decimal to binary) Write a recursive method that...Ch. 18 - (Decimal to hex) Write a recursive method that...Ch. 18 - (Binary to decimal) Write a recursive method that...Ch. 18 - (Hex to decimal) Write a recursive method that...Ch. 18 - Prob. 18.25PECh. 18 - (Create a maze) Write a program that will find a...Ch. 18 - (Koch snowflake fractal) The text presented the...Ch. 18 - (Nonrecursive directory size) Rewrite Listing...Ch. 18 - (Number of files in a directory) Write a program...Ch. 18 - (Game: Knights Tour) The Knights Tour is an...Ch. 18 - (Game: Knights Tour animation) Write a program for...Ch. 18 - (Game: Eight Queens) The Eight Queens problem is...Ch. 18 - Prob. 18.35PECh. 18 - (Sierpinski triangle) Write a program that lets...Ch. 18 - (Hilbert curve) The Hilbert curve, first described...Ch. 18 - (Recursive tree) Write a program to display a...Ch. 18 - Prob. 18.39PE
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Suppose v is an object of the class vectorint. Use the search generic function (Display 18.18) to write some co...
Problem Solving with C++ (10th Edition)
Bug Collector The Bug Collector Problem A bug collector collects bugs every day for five days. Write a program ...
Starting Out with Python (4th Edition)
Write a complete Java program that behaves as follows. The program displays an input dialog window asking the u...
Java: An Introduction to Problem Solving and Programming (8th Edition)
Determine the components of the force acting parallel and perpendicular to the axis of the pole. Prob. F2-30
INTERNATIONAL EDITION---Engineering Mechanics: Statics, 14th edition (SI unit)
59. A 100-watt [W] motor (60% efficient) is used to raise a 100-kilogram [kg] toad 5 meters [m] into the air. H...
Thinking Like an Engineer: An Active Learning Approach (4th Edition)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- Which of the following statements are correct? Iteration is always worse than recursion. Recursion uses more memory than an iterative approach. Recursion uses less memory than an iterative approach. O Iterative function is always easier to write than recursion.arrow_forwardplease write Java code for the following solutionarrow_forward1. Let product(n,m) be a recursive addition-subtraction method for multiplying two positive integers. Recursive cases for m = 1 and m < 1 make this method. The return value should be n plus a recursive product() call with n and m - 1. Test a Java method.arrow_forward
- Provide JAVA source code with proper comments for attached assginment.arrow_forward12 - question The following method is a recursive pow method to compute exponents, there is a logical error in this code. Please choose the line which has the error. 1. public static int pow (int x, int y) { 2. if (y>1) 3. return x * pow (x, y - 1); 4. else 5. return y; 6. } a. Line 2 b. Line 3 C. Line 4 d. Line 5arrow_forwardEx. 01 : Recursion About So far, we have learned that we can perform repetitive tasks using loops. However, another way is by creating methods that call themselves. This programming technique is called recursion and, a method that calls itself within it's own body is called a recursive method. One use of recursion is to perform repetitive tasks instead of using loops, since some problems seem to be solved more naturally with recursion than with loops. To solve a problem using recursion, it is broken down into sub-problems. Each sub-problem is similar to the original problem, but smaller in size. You can apply the same approach to each sub-problem to solve it recursively. All recursive methods use conditional tests to either 1. stop or 2. continue the recursion. Each recursive method has the following characteristics: 1. end/terminating case: One or more end cases to stop the recursion. 2. recursive case: reduces the problem in to smaller sub-problems, until it reaches (becomes) the end…arrow_forward
- a) Write a recursive method that calculates the following series: F(n)= (n1+i)ni=1*(n2+i)n/2i=1*(n4+i)n/4i=1*(n8+i)n/8i=1*....*2 b) Write the recurrence expression T(n).arrow_forwardPlease convert Mutual Recursion to Java. See attached image. Thank you.arrow_forwardIt is suspected that out of a set of 64 50p coins one of the coins is fake (i.e., lighter in weight than a genuine coin). With one weighing scale. (i)Give a detailed explanation on how you would go about determining the fake coin. (ii)What is the minimum number of times you need to use the scales to weigh the coins before identifying the fake coin? (iii)Write a recursive method that returns the value of N! (N factorial). Explain why you would not normally use recursion to solve this problem.arrow_forward
- Compute the combinations of n things taken k at a time using both iterative and recursive methods. Both methods should use only int types for mathematical operations. If the iterative method continues to mock, that is fine, the recursive method should not. Can you please modify the following code for the given question?(Java Programming) public class Combination { private static boolean useFact; /*** Computes some combinations.* * @param args unused*/public static void main(String[] args) {for (int i = 0; i < 2; i++) {useFact = (i % 2 == 0);System.out.println(useFact ? "FACTORIAL METHOD" : "RECURSIVE METHOD");System.out.println();combination(2, 2);combination(3, 2);combination(4, 3);combination(10, 2);combination(52, 5);combination(60, 4);combination(75, 3);combination(100, 4);System.out.println();}} /*** A method which switches between the two combination methods* @param n the number of things* @param k how many to choose*/public static void combination(int n, int k)…arrow_forwarda. Prompt the user to type a word on console and save the input into variable b. Implement recursive method(s) isPalindrome() to check if the given word is a palindrome and call it from the main(). c. Implement recursive method(s) theLength() to calculate the length of the given word (its number of characters) d. Display your results on console in the following format: "You word palindrome." consists of_ characters, this a e. Explain in comments the algorithms you use (how the recursion works, where is the base case, there is the recursive call(s), how you reduce the problem complexity).arrow_forwardThe solution must be recursive sumNRec: The method takes an integer array A and returns the sum of all integers in the parameter array.arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
1.1 Arrays in Data Structure | Declaration, Initialization, Memory representation; Author: Jenny's lectures CS/IT NET&JRF;https://www.youtube.com/watch?v=AT14lCXuMKI;License: Standard YouTube License, CC-BY
Definition of Array; Author: Neso Academy;https://www.youtube.com/watch?v=55l-aZ7_F24;License: Standard Youtube License