-3

I want to do division but with subtraction. I also don't necessarily want the exact answer. No floating point numbers too (preferably) How can this be achieved? Thanks in advance:)

Also the process should almost be as fast as normal division.

dickson
  • 11
  • 3
  • 2
    An approximation to division using iterative subtraction is highly unlikely to be as fast as straight division - even in Python – DarkKnight Sep 02 '22 at 16:45
  • Can you give more details about what this is for? Where does this problem come from? Specifically, why do you need this to be "almost as fast as normal division", and why do you need to use subtraction? You have no hope of a pure Python subtraction-based solution coming close to the speed of the C-based division algorithm that underlies Python's `/` operator. – Mark Dickinson Sep 04 '22 at 14:57

2 Answers2

0

to approximate x divided by y you can subtract y from x until the result is smaller or equal to 0 and then the result of the division would be the number of times you subtracted y from x. However this doesn't work with negatives numbers.

Cadeyrn
  • 545
  • 2
  • 12
0

Well, let's say you have your numerator and your denominator. The division basically consists in estimating how many denominator you have in your numerator.

So a simple loop should do:

def divide_by_sub(numerator, denominator):

    # Init
    result = 0
    remains = numerator
    
    # Substract as much as possible
    while remains >= denominator:
        remains -= denominator
        result += 1
    
    # Here we have the "floor" part of the result
    return result

This will give you the "floor" part of your result. Please consider adding some guardrails to handle "denominator is zero", "numerator is negative", etc.

My best guess, if you want go further, would be to then add an argument to the function for the precision you want like precision and then multiply remains by it (for instance 10 or 100), and reloop on it. It's doable recursively:

def divide_by_sub(numerator, denominator, precision):

    # Init
    result = 0
    remains = numerator

    # Substract as much as possible
    while remains >= denominator:
        remains -= denominator
        result += 1

    # Here we have the "floor" part of the result. We proceed to more digits
    if precision > 1:
        remains = remains * precision
        float_result = divide_by_sub(remains, denominator, 1)
        result += float_result/precision

    return result

Giving you, for instance for divide_by_sub(7,3,1000) the following:

2.333

Limtorak
  • 81
  • 5
  • Wow. Thanks. But will it be as fast as normal division? Considering big numbers (say a 100 digits) – dickson Sep 02 '22 at 17:38
  • Honestly I don't know. Operations are quite deeply embedded in os so I would say my code would be slower. If you want to know, I advise you loop extensively (several millions of iterations) on it and track the time both techniques took.. – Limtorak Sep 03 '22 at 18:06
  • Please consider giving us feedbacks if you can, as well as rating answers/closing topic ;) – Limtorak Sep 05 '22 at 08:20