Recursive function that returns the remainder









up vote
0
down vote

favorite












I am instructed to define a recursive function in Python that finds the remainder of n divided by b with the condition to not use the "/" ,"%" or "//" operator. I have defined the following function, which works fine for positive numbers. Is there a better way to do this using recursion and simple conditions.



def division(n, b, q = 1):
"""
parameters : a et b (integers)
returns: the remainder of a and b
pre-requisites : q = 1
"""
if n <= 0 or n < b:
if n == 0:
print("Your division has no remainder.")
elif n in range(0,5):
print("Your remainder is", n)
return 0
else:
return division(n - b, b, q) + q

print(division(274,5))









share|improve this question























  • are you allowed to use the % operator? ...
    – hiro protagonist
    Nov 10 at 12:25











  • sorry,no, you may not, I will edit the question now.
    – Mister Tusk
    Nov 10 at 12:27










  • @hiroprotagonist He is "instructed to define a recursive function" ... So I would not consider a one-liner solution with "%". (As always when it comes to homework questions)
    – quant
    Nov 10 at 12:27










  • Are you looking for a way to extend this to negative numbers? Your function looks recursive and simple to me. Note sure about that elif part though.
    – kabanus
    Nov 10 at 12:30











  • i was trying to be sarcastic... that went the wrong way. sorry.
    – hiro protagonist
    Nov 10 at 12:31














up vote
0
down vote

favorite












I am instructed to define a recursive function in Python that finds the remainder of n divided by b with the condition to not use the "/" ,"%" or "//" operator. I have defined the following function, which works fine for positive numbers. Is there a better way to do this using recursion and simple conditions.



def division(n, b, q = 1):
"""
parameters : a et b (integers)
returns: the remainder of a and b
pre-requisites : q = 1
"""
if n <= 0 or n < b:
if n == 0:
print("Your division has no remainder.")
elif n in range(0,5):
print("Your remainder is", n)
return 0
else:
return division(n - b, b, q) + q

print(division(274,5))









share|improve this question























  • are you allowed to use the % operator? ...
    – hiro protagonist
    Nov 10 at 12:25











  • sorry,no, you may not, I will edit the question now.
    – Mister Tusk
    Nov 10 at 12:27










  • @hiroprotagonist He is "instructed to define a recursive function" ... So I would not consider a one-liner solution with "%". (As always when it comes to homework questions)
    – quant
    Nov 10 at 12:27










  • Are you looking for a way to extend this to negative numbers? Your function looks recursive and simple to me. Note sure about that elif part though.
    – kabanus
    Nov 10 at 12:30











  • i was trying to be sarcastic... that went the wrong way. sorry.
    – hiro protagonist
    Nov 10 at 12:31












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am instructed to define a recursive function in Python that finds the remainder of n divided by b with the condition to not use the "/" ,"%" or "//" operator. I have defined the following function, which works fine for positive numbers. Is there a better way to do this using recursion and simple conditions.



def division(n, b, q = 1):
"""
parameters : a et b (integers)
returns: the remainder of a and b
pre-requisites : q = 1
"""
if n <= 0 or n < b:
if n == 0:
print("Your division has no remainder.")
elif n in range(0,5):
print("Your remainder is", n)
return 0
else:
return division(n - b, b, q) + q

print(division(274,5))









share|improve this question















I am instructed to define a recursive function in Python that finds the remainder of n divided by b with the condition to not use the "/" ,"%" or "//" operator. I have defined the following function, which works fine for positive numbers. Is there a better way to do this using recursion and simple conditions.



def division(n, b, q = 1):
"""
parameters : a et b (integers)
returns: the remainder of a and b
pre-requisites : q = 1
"""
if n <= 0 or n < b:
if n == 0:
print("Your division has no remainder.")
elif n in range(0,5):
print("Your remainder is", n)
return 0
else:
return division(n - b, b, q) + q

print(division(274,5))






python function recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 12:27

























asked Nov 10 at 12:15









Mister Tusk

1059




1059











  • are you allowed to use the % operator? ...
    – hiro protagonist
    Nov 10 at 12:25











  • sorry,no, you may not, I will edit the question now.
    – Mister Tusk
    Nov 10 at 12:27










  • @hiroprotagonist He is "instructed to define a recursive function" ... So I would not consider a one-liner solution with "%". (As always when it comes to homework questions)
    – quant
    Nov 10 at 12:27










  • Are you looking for a way to extend this to negative numbers? Your function looks recursive and simple to me. Note sure about that elif part though.
    – kabanus
    Nov 10 at 12:30











  • i was trying to be sarcastic... that went the wrong way. sorry.
    – hiro protagonist
    Nov 10 at 12:31
















  • are you allowed to use the % operator? ...
    – hiro protagonist
    Nov 10 at 12:25











  • sorry,no, you may not, I will edit the question now.
    – Mister Tusk
    Nov 10 at 12:27










  • @hiroprotagonist He is "instructed to define a recursive function" ... So I would not consider a one-liner solution with "%". (As always when it comes to homework questions)
    – quant
    Nov 10 at 12:27










  • Are you looking for a way to extend this to negative numbers? Your function looks recursive and simple to me. Note sure about that elif part though.
    – kabanus
    Nov 10 at 12:30











  • i was trying to be sarcastic... that went the wrong way. sorry.
    – hiro protagonist
    Nov 10 at 12:31















are you allowed to use the % operator? ...
– hiro protagonist
Nov 10 at 12:25





are you allowed to use the % operator? ...
– hiro protagonist
Nov 10 at 12:25













sorry,no, you may not, I will edit the question now.
– Mister Tusk
Nov 10 at 12:27




sorry,no, you may not, I will edit the question now.
– Mister Tusk
Nov 10 at 12:27












@hiroprotagonist He is "instructed to define a recursive function" ... So I would not consider a one-liner solution with "%". (As always when it comes to homework questions)
– quant
Nov 10 at 12:27




@hiroprotagonist He is "instructed to define a recursive function" ... So I would not consider a one-liner solution with "%". (As always when it comes to homework questions)
– quant
Nov 10 at 12:27












Are you looking for a way to extend this to negative numbers? Your function looks recursive and simple to me. Note sure about that elif part though.
– kabanus
Nov 10 at 12:30





Are you looking for a way to extend this to negative numbers? Your function looks recursive and simple to me. Note sure about that elif part though.
– kabanus
Nov 10 at 12:30













i was trying to be sarcastic... that went the wrong way. sorry.
– hiro protagonist
Nov 10 at 12:31




i was trying to be sarcastic... that went the wrong way. sorry.
– hiro protagonist
Nov 10 at 12:31












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










I believe your teacher was probably only trying to go for remainders without quotients.



def division(n, b):
if n < b:
return n
return division(n - b, b)
print(division(274, 5))


However, since you brought it up, you can do it with the quotient, without having to start with a 1 for the default.



def division(n, b, q = 0):
if n < b:
return n, q
return division(n - b, b, q + 1)
print(division(274, 5))


Main takeaways, you do not need to check n for range (0,5).






share|improve this answer








New contributor




Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    2
    down vote













    What about



    def remainder(n, q):
    if(n < q):
    return n
    return remainder(n - q, q)

    print(remainder(274, 5)) # will return: 4
    print(remainder(275, 5)) # will return: 0
    print(remainder(123, 3)) # will return: 0


    much shorter ...






    share|improve this answer




















    • That's definitely better, thanks! how would you extend this to work for negative numbers?
      – Mister Tusk
      Nov 10 at 12:34










    • @MisterTusk That depends a bit, as the modulo of negative numbers is not too well defined. There are different ways of calculating them with different results, see the excellent answer of chux here stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
      – quant
      Nov 10 at 12:41










    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238859%2frecursive-function-that-returns-the-remainder%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    I believe your teacher was probably only trying to go for remainders without quotients.



    def division(n, b):
    if n < b:
    return n
    return division(n - b, b)
    print(division(274, 5))


    However, since you brought it up, you can do it with the quotient, without having to start with a 1 for the default.



    def division(n, b, q = 0):
    if n < b:
    return n, q
    return division(n - b, b, q + 1)
    print(division(274, 5))


    Main takeaways, you do not need to check n for range (0,5).






    share|improve this answer








    New contributor




    Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      2
      down vote



      accepted










      I believe your teacher was probably only trying to go for remainders without quotients.



      def division(n, b):
      if n < b:
      return n
      return division(n - b, b)
      print(division(274, 5))


      However, since you brought it up, you can do it with the quotient, without having to start with a 1 for the default.



      def division(n, b, q = 0):
      if n < b:
      return n, q
      return division(n - b, b, q + 1)
      print(division(274, 5))


      Main takeaways, you do not need to check n for range (0,5).






      share|improve this answer








      New contributor




      Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.



















        up vote
        2
        down vote



        accepted







        up vote
        2
        down vote



        accepted






        I believe your teacher was probably only trying to go for remainders without quotients.



        def division(n, b):
        if n < b:
        return n
        return division(n - b, b)
        print(division(274, 5))


        However, since you brought it up, you can do it with the quotient, without having to start with a 1 for the default.



        def division(n, b, q = 0):
        if n < b:
        return n, q
        return division(n - b, b, q + 1)
        print(division(274, 5))


        Main takeaways, you do not need to check n for range (0,5).






        share|improve this answer








        New contributor




        Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        I believe your teacher was probably only trying to go for remainders without quotients.



        def division(n, b):
        if n < b:
        return n
        return division(n - b, b)
        print(division(274, 5))


        However, since you brought it up, you can do it with the quotient, without having to start with a 1 for the default.



        def division(n, b, q = 0):
        if n < b:
        return n, q
        return division(n - b, b, q + 1)
        print(division(274, 5))


        Main takeaways, you do not need to check n for range (0,5).







        share|improve this answer








        New contributor




        Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered Nov 10 at 12:37









        Paritosh Singh

        2016




        2016




        New contributor




        Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        Paritosh Singh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            up vote
            2
            down vote













            What about



            def remainder(n, q):
            if(n < q):
            return n
            return remainder(n - q, q)

            print(remainder(274, 5)) # will return: 4
            print(remainder(275, 5)) # will return: 0
            print(remainder(123, 3)) # will return: 0


            much shorter ...






            share|improve this answer




















            • That's definitely better, thanks! how would you extend this to work for negative numbers?
              – Mister Tusk
              Nov 10 at 12:34










            • @MisterTusk That depends a bit, as the modulo of negative numbers is not too well defined. There are different ways of calculating them with different results, see the excellent answer of chux here stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
              – quant
              Nov 10 at 12:41














            up vote
            2
            down vote













            What about



            def remainder(n, q):
            if(n < q):
            return n
            return remainder(n - q, q)

            print(remainder(274, 5)) # will return: 4
            print(remainder(275, 5)) # will return: 0
            print(remainder(123, 3)) # will return: 0


            much shorter ...






            share|improve this answer




















            • That's definitely better, thanks! how would you extend this to work for negative numbers?
              – Mister Tusk
              Nov 10 at 12:34










            • @MisterTusk That depends a bit, as the modulo of negative numbers is not too well defined. There are different ways of calculating them with different results, see the excellent answer of chux here stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
              – quant
              Nov 10 at 12:41












            up vote
            2
            down vote










            up vote
            2
            down vote









            What about



            def remainder(n, q):
            if(n < q):
            return n
            return remainder(n - q, q)

            print(remainder(274, 5)) # will return: 4
            print(remainder(275, 5)) # will return: 0
            print(remainder(123, 3)) # will return: 0


            much shorter ...






            share|improve this answer












            What about



            def remainder(n, q):
            if(n < q):
            return n
            return remainder(n - q, q)

            print(remainder(274, 5)) # will return: 4
            print(remainder(275, 5)) # will return: 0
            print(remainder(123, 3)) # will return: 0


            much shorter ...







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 12:30









            quant

            1,16811025




            1,16811025











            • That's definitely better, thanks! how would you extend this to work for negative numbers?
              – Mister Tusk
              Nov 10 at 12:34










            • @MisterTusk That depends a bit, as the modulo of negative numbers is not too well defined. There are different ways of calculating them with different results, see the excellent answer of chux here stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
              – quant
              Nov 10 at 12:41
















            • That's definitely better, thanks! how would you extend this to work for negative numbers?
              – Mister Tusk
              Nov 10 at 12:34










            • @MisterTusk That depends a bit, as the modulo of negative numbers is not too well defined. There are different ways of calculating them with different results, see the excellent answer of chux here stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
              – quant
              Nov 10 at 12:41















            That's definitely better, thanks! how would you extend this to work for negative numbers?
            – Mister Tusk
            Nov 10 at 12:34




            That's definitely better, thanks! how would you extend this to work for negative numbers?
            – Mister Tusk
            Nov 10 at 12:34












            @MisterTusk That depends a bit, as the modulo of negative numbers is not too well defined. There are different ways of calculating them with different results, see the excellent answer of chux here stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
            – quant
            Nov 10 at 12:41




            @MisterTusk That depends a bit, as the modulo of negative numbers is not too well defined. There are different ways of calculating them with different results, see the excellent answer of chux here stackoverflow.com/questions/13683563/… . If you tell me which way you want, I will update my answer accordingly.
            – quant
            Nov 10 at 12:41

















             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238859%2frecursive-function-that-returns-the-remainder%23new-answer', 'question_page');

            );

            Post as a guest














































































            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto