div_euclidienne.py

Created by alexandrebret-84

Created on November 18, 2021

1.05 KB

Résout une division euclidienne.


def diveuc(a, b): # a = dividende, b = diviseur
    q = 0
    if b == 0:
        raise ValueError("on ne peut pas diviser pas 0")
    elif a == 0:
        return (0, 0)
    elif a > 0 and b > 0:
        r = a
        while b <= r:
            r -= b
            q += 1
        return (q, r)
    elif a < 0 and b > 0:
        r = -a
        while b <= r:
            r -= b
            q -= 1
        if r != 0:
            q -= 1
            r = abs(r - b)
        return (q, r)
    elif a > 0 and b < 0:
        r = a
        while -b <= r:
            r -= -b
            q -= 1
        return (q, r)
    else:
        r = -a
        while -b <= r:
            r -= -b
            q += 1
        if r != 0:
            q += 1
            r = abs(r + b)
        return (q, r)
    
    
def diveuc_fraude(a, b):
    if b == 0:
        raise ValueError("on ne peut pas diviser pas 0")
    elif a == 0:
        return (0, 0)
    elif b > 0:
        return (a//b, a%b)
    elif b < 0:
        return ((a//b) + 1, (a%b) - b) if a%b != 0 else (a//b, a%b)

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.