1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
dem82 [27]
3 years ago
8

For two integers m and n, their GCD(Greatest Common Divisor) can be computed by a recursive function. Write a recursive method g

cd(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, your program should follow the instructions of this question, otherwise you will not get the credit. Then write a testing program to call the recursive method.
Computers and Technology
1 answer:
konstantin123 [22]3 years ago
6 0

Answer:

In Python:

def gcd(m,n):

if n == 0:

 return m

elif m == 0:

    return n

else:

 return gcd(n,m%n)

Explanation:

This defines the function

def gcd(m,n):

If n is 0, return m

<em> if n == 0: </em>

<em>  return m </em>

If m is 0, return n

<em> elif m == 0: </em>

<em>     return n </em>

If otherwise, calculate the gcd recursively

<em> else: </em>

<em>  return gcd(n,m%n)</em>

<em />

<em>To call the function to calculate the gcd of say 15 and 5 from main, use:</em>

<em>gcd(15,5)</em>

You might be interested in
Writing anything that comes to mind that may relate to your personal essay is called:
sammy [17]
My life career in computer science
7 0
3 years ago
Read 2 more answers
The IT infrastructure components should be evaluated based on their expected financial value. All of the following are used to q
vladimir2022 [97]

Answer:(A) usability

Explanation:

The term usability refers to the usage value of products, how much they can be used and their value after their use. So adding on these points usability helps to quantify the financial value of IT components.

5 0
3 years ago
Jorge is sending a large image file to a friend as part of a shared classroom project. Which of the following is most likely tru
madreJ [45]

Answer:

D. The image will require fewer bits in order to be represented.

Explanation:

Lossy compression algorithms are typically better than lossless compression algorithms at reducing the number of bits needed to represent a piece of data.

5 0
2 years ago
Which education level has the highest return on investment (ROI)?
Keith_Richards [23]
The correct option is B.
Having a bachelor degree from a college gives the highest returns on investments. People who go to college usually secure high paying jobs compare to their counterparts who have less education.
The type of college one attends also matter, for instance in USA, the college in the first position, which gives the highest return on investment is Massachusetts Institute of Technology  [MIT].
5 0
3 years ago
Read 2 more answers
(Displaying a Sentence with Its Words Reversed) Write an application that inputs a line of text, tokenizes the line with String
Serjik [45]

Answer:

I am writing a JAVA and Python program. Let me know if you want the program in some other programming language.

import java.util.Scanner; // class for taking input from user

public class Reverse{ //class to reverse a sentence

public static void main(String[] args) { //start of main() function body

Scanner input = new Scanner(System.in); //create a Scanner class object

   System.out.print("Enter a sentence: "); //prompts user to enter a sentence

   String sentence = input.nextLine(); // scans and reads input sentence

   String[] tokens = sentence.split(" "); // split the sentence into tokens using split() method and a space as delimiter

   for (int i = tokens.length - 1; i >= 0; i--) { //loop that iterates through the tokens of the sentence in reverse order

       System.out.println(tokens[i]); } } } // print the sentence tokens in reverse order

Explanation:

In JAVA program the user is asked to input a sentence. The sentence is then broken into tokens using split() method. split method returns an array of strings after splitting or breaking the sentence based on the delimiter. Here the delimiter used is space characters. So split() method splits the sentence into tokens based on the space as delimiter to separate the words in the sentence. Next the for loop is used to iterate through the tokens as following:

Suppose the input sentence is "How are you"

After split() method it becomes:

How

are

you

Next the loop has a variable i which initialized to the tokens.length-1. This loop will continue to execute till the value of i remains greater than or equals to 0.

for (int i = tokens.length - 1; i >= 0; i--)

The length of first token i.e you is 3 so the loop executes and prints the word you.

Then at next iteration the value of i is decremented by 1 and now it points at the token are and prints the word are

Next iteration the value of i is again decremented by i and it prints the word How.

This can be achieved in Python as:

def reverse(sentence):

   rev_tokens = ' '.join(reversed(sentence.split(' ')))

   return rev_tokens

   

line = input("enter a sentence: ")

print((reverse(line)))

The method reverse takes a sentence as parameter. Then rev_tokens = ' '.join(reversed(sentence.split(' ')))  statement first splits the sentence into array of words/tokens using space as a delimiter. Next reversed() method returns the reversed sentence.  Next the join() method join the reversed words of the sentence using a space separator ' '.join(). If you want to represent the reversed tokens each in a separate line then change the above statement as follows:

rev_tokens = '\n'.join(reversed(sentence.split(' ')))  

Here the join method joins the reversed words separating them with a new line.

Simply input a line of text and call this reverse() method by passing the input line to it.

4 0
2 years ago
Other questions:
  • Which of the following is not an algorithm?
    8·1 answer
  • _ includes websites that encourage interaction and connection among people, businesses, and organizations.
    5·1 answer
  • Can i have information about skrillex hobbies
    6·1 answer
  • What is used to monitor the activity of a network and notify network administrators when it identifies something as suspicious?
    15·1 answer
  • Fred wants to analyze his spending habits of the past few years and has gathered information on the checks he has written from 2
    6·1 answer
  • I will upvote....
    10·1 answer
  • 9. Which of the following is the most popular eSport game ever?
    13·1 answer
  • Which type of evaluation requires that the program be fully implemented before the evaluation can begin
    9·1 answer
  • You are a librarian! Ask the user for the last names of the authors of the five books they are returning. Print a list of those
    15·1 answer
  • If a company saw an online photo of you playing basketball, it might try to sell
    5·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!