Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8561891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:36:24+00:00 2026-06-11T16:36:24+00:00

I am given the function gcd, which is defined as follows: def gcd(a, b):

  • 0

I am given the function gcd, which is defined as follows:

def gcd(a, b):
    if (0 == a % b):
        return b
    return gcd(b, a%b)

Now I am asked to write a recursive function gcd2(a,b) that returns a list of three numbers (g, s, t) where g = gcd(a, b) and g = s*a + t*b.

This means that you would enter two values (a and b) into the gcd(a, b) function. The value it returns equals g in the next function.

These same a and b values are then called into gcd2(a, b). The recursive part is then used to find the values for s and t so that g = s*a + t*b.

I am not sure how to approach this because I can’t really envision what the “stopping-condition” would be, or what exactly I’d be looping through recursively to actually find s and t. Can anyone help me out?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:36:26+00:00Added an answer on June 11, 2026 at 4:36 pm

    The key insight is that we can work backwards, finding s and t for each a and b in the recursion. So say we have a = 21 and b = 15. We need to work through each iteration, using several values — a, b, b % a, and c where a = c * b + a % b. First, let’s consider each step of the basic GCD algorithm:

    21 = 1 * 15 + 6
    15 = 2 * 6  + 3
    6  = 2 * 3  + 0 -> end recursion
    

    So our gcd (g) is 3. Once we have that, we determine s and t for 6 and 3. To do so, we begin with g, expressing it in terms of (a, b, s, t = 3, 0, 1, -1):

    3  = 1 * 3 + -1 * 0
    

    Now we want to eliminate the 0 term. From the last line of the basic algorithm, we know that 0 = 6 – 2 * 3:

    3 = 1 * 3 + -1 * (6 - 2 * 3)
    

    Simplifying, we get

    3 = 1 * 3 + -1 * 6 + 2 * 3
    3 = 3 * 3 + -1 * 6
    

    Now we swap the terms:

    3 = -1 * 6 + 3 * 3
    

    So we have s == -1 and t == 3 for a = 6 and b = 3. So given those values of a and b, gcd2 should return (3, -1, 3).

    Now we step back up through the recursion, and we want to eliminate the 3 term. From the next-to-last line of the basic algorithm, we know that 3 = 15 – 2 * 6. Simplifying and swapping again (slowly, so that you can see the steps clearly…):

    3 = -1 * 6 + 3 * (15 - 2 * 6)
    3 = -1 * 6 + 3 * 15 - 6 * 6
    3 = -7 * 6 + 3 * 15
    3 = 3 * 15 + -7 * 6
    

    So for this level of recursion, we return (3, 3, -7). Now we want to eliminate the 6 term.

    3 = 3 * 15 + -7 * (21 - 1 * 15)
    3 = 3 * 15 + 7 * 15 - 7 * 21
    3 = 10 * 15 - 7 * 21
    3 = -7 * 21 + 10 * 15
    

    And voila, we have calculated s and t for 21 and 15.

    So schematically, the recursive function will look like this:

    def gcd2(a, b):
        if (0 == a % b):
            # calculate s and t
            return b, s, t
        else:
            g, s, t = gcd2(b, a % b)
            # calculate new_s and new_t
            return g, new_s, new_t
    

    Note that for our purposes here, using a slightly different base case simplifies things:

    def gcd2(a, b):
        if (0 == b):
            return a, 1, -1
        else:
            g, s, t = gcd2(b, a % b)
            # calculate new_s and new_t
            return g, new_s, new_t
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Filter function returns a sublist of elements which return true for a given function.
I'm coding a small class that maximizes a given function F and returns the
Please tell me what will the call to given function return and how? The
Given a function, I'd like to know whether it's a developer-defined function or a
Given a function object, how can I get its signature? For example, for: def
I am given a function rand5() that generates, with a uniform distribution, a random
Consider the following wrapper function that retrys a given function some given number of
if i have a function A,which can apply a certain rule on a given
Possible Duplicate: In Perl, how can I check from which module a given function
Possible Duplicate: In Perl, how can I check from which module a given function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.