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 7638841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:16:17+00:00 2026-05-31T08:16:17+00:00

I am new to python and I am having a problem. I want to

  • 0

I am new to python and I am having a problem. I want to write a recursive function that takes two inputs (integers) and subtracts the second from the first, until the first is less than the second, and calculates the # of time it subtracts before being less.

This is what I have so far, but I am having problems getting the function to repeat the subtraction of the second from the first;

def div(first,sec):
    if first > sec:
        return div((first - sec),sec) + first

    else:
        return 0
  • 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-05-31T08:16:18+00:00Added an answer on May 31, 2026 at 8:16 am
    ## naive recursion
    def div(a, b):
        if (a >= b):
            return div(a - b, b) + 1
        else: return 0
    
    try:
        print div(5678, 3)
    except Exception as e:
        print e  ## maximum recursion depth exceeded
    
    
    ## less naive recursion with trampolines
    ## see https://gist.github.com/802557 for details/explanations
    def trampoline(f):
        def _(*args):
            result = f(*args)
            while callable(result):
                result = result()
            return result
        return _
    
    def div_func(a, b, acc=0):
        if (a >= b):
            return lambda: div_func(a - b, b, acc + 1)
        else: return acc
    
    div2 = trampoline(div_func)
    
    ## ok
    print div2(5678, 3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I am a new user to Python and I am having problem doing
I'm very new to Python development, and am having a problem with one of
I'm pretty new to Python, so hopefully the problem I'm having has a simple
The problem I am having at this point in time (being new to Python)
I'm relatively new to Python and am having problems programming with Scapy, the Python
I'm a new Python programmer who is having a little trouble using 'self' in
I am extremely new to python, having started to learn it less than a
I'm using new style classes in Python 2.6 and am having trouble with __getattr__
Im new two python and am trying to grow a dictionary of dictionaries. I
I'm having an issue with a rather intricate interaction of C++ and Python that

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.