How to write 2**n - 1 as a recursive function?What is “Total Functional Programming”?What is tail recursion?How do I merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?Using global variables in a functionHow to make a chain of function decorators?“Least Astonishment” and the Mutable Default ArgumentHow do I list all files of a directory?

What can my hide armor look like?

Physical interpretation of complex numbers

What is this sound, instrument? (Rheingold, Wagner)

What is the difference between turbojet and turbofan engines?

Was the whistle-blower's (12 Aug 2019) complaint deemed credible?

Improving the observation skill & making less blunders

MS in Mathematics, having trouble finding work outside teaching algebra

A professor commented that my research is too simple as compared to my colleagues. What does that mean about my future prospects?

Could the Ancient Egyptian hieroglyphs have been deciphered without the Rosetta Stone with modern tech?

Ball-passing game with a surprise ending

Need Good OOP Design For World and Countries Problem

Good way to backup laptop SSD with UEFI, Ubuntu, Windows dual boot

What would happen to the world if all lightning stopped?

What's the name of the role of characters who buff teammates?

Printing the bits of an integer using bitfields and union

Surfacing out of a sunken ship/submarine - Survival Tips

Explanation needed for why my solution is incorrect.

What was the first operating system that supported preemptive multitasking?

Do airplanes need brakes in the air?

Slimy whey in store-bought yoghurt

How to start toward financial independence

What deck size is ideal?

What is the role of Shutter Speed while using a Manual Flash?

What is the meaning of "Ya con hotra y como se yama mijo"?



How to write 2**n - 1 as a recursive function?


What is “Total Functional Programming”?What is tail recursion?How do I merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?Using global variables in a functionHow to make a chain of function decorators?“Least Astonishment” and the Mutable Default ArgumentHow do I list all files of a directory?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









45

















I need a function that takes n and returns 2n - 1 . It sounds simple enough, but the function has to be recursive. So far I have just 2n:



def required_steps(n):
if n == 0:
return 1
return 2 * req_steps(n-1)


The exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"










share|improve this question























  • 4





    Just for the record, it should be vastly more efficient to do it like a normal person with a shift and subtract. Python integers are arbitrary width so 1 << n can't overflow. This seems to be an exercise in inventing a way to decompose (1<<n) - 1 into multiple steps, perhaps setting each bit one at a time like some answers show.

    – Peter Cordes
    Oct 14 at 23:57






  • 8





    def fn(n): if n == 0: return 1; return (2 << n) - fn(0); # technically recursive

    – MooseBoys
    Oct 15 at 6:28






  • 3





    @Voo: Not Carl, but please list me everything that is contained in C:MyFolder

    – Flater
    Oct 16 at 11:11






  • 1





    @Voo: Dependency or not is irrelevant for an exercise which purely focuses on teaching the concept of recursion. I could make a basic mocked set of classes/methods that students could use. You're focusing on something that is completely besides the point of the exercise. Using file system navigation is a good example because students generally understand the inherently recurrent nature of folders and files (i.e. folders can be nested in each other nigh indefinitely)

    – Flater
    Oct 16 at 11:47







  • 1





    @Voo No I'm saying that you can teach recursion by showing a recursive data structure. I have no idea why you struggle grasping this.

    – Flater
    Oct 16 at 21:46

















45

















I need a function that takes n and returns 2n - 1 . It sounds simple enough, but the function has to be recursive. So far I have just 2n:



def required_steps(n):
if n == 0:
return 1
return 2 * req_steps(n-1)


The exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"










share|improve this question























  • 4





    Just for the record, it should be vastly more efficient to do it like a normal person with a shift and subtract. Python integers are arbitrary width so 1 << n can't overflow. This seems to be an exercise in inventing a way to decompose (1<<n) - 1 into multiple steps, perhaps setting each bit one at a time like some answers show.

    – Peter Cordes
    Oct 14 at 23:57






  • 8





    def fn(n): if n == 0: return 1; return (2 << n) - fn(0); # technically recursive

    – MooseBoys
    Oct 15 at 6:28






  • 3





    @Voo: Not Carl, but please list me everything that is contained in C:MyFolder

    – Flater
    Oct 16 at 11:11






  • 1





    @Voo: Dependency or not is irrelevant for an exercise which purely focuses on teaching the concept of recursion. I could make a basic mocked set of classes/methods that students could use. You're focusing on something that is completely besides the point of the exercise. Using file system navigation is a good example because students generally understand the inherently recurrent nature of folders and files (i.e. folders can be nested in each other nigh indefinitely)

    – Flater
    Oct 16 at 11:47







  • 1





    @Voo No I'm saying that you can teach recursion by showing a recursive data structure. I have no idea why you struggle grasping this.

    – Flater
    Oct 16 at 21:46













45












45








45


8






I need a function that takes n and returns 2n - 1 . It sounds simple enough, but the function has to be recursive. So far I have just 2n:



def required_steps(n):
if n == 0:
return 1
return 2 * req_steps(n-1)


The exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"










share|improve this question

















I need a function that takes n and returns 2n - 1 . It sounds simple enough, but the function has to be recursive. So far I have just 2n:



def required_steps(n):
if n == 0:
return 1
return 2 * req_steps(n-1)


The exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"







python recursion






share|improve this question
















share|improve this question













share|improve this question




share|improve this question








edited Oct 15 at 5:39









selbie

61.9k11 gold badges69 silver badges137 bronze badges




61.9k11 gold badges69 silver badges137 bronze badges










asked Oct 14 at 14:08









KajiceKajice

3592 silver badges9 bronze badges




3592 silver badges9 bronze badges










  • 4





    Just for the record, it should be vastly more efficient to do it like a normal person with a shift and subtract. Python integers are arbitrary width so 1 << n can't overflow. This seems to be an exercise in inventing a way to decompose (1<<n) - 1 into multiple steps, perhaps setting each bit one at a time like some answers show.

    – Peter Cordes
    Oct 14 at 23:57






  • 8





    def fn(n): if n == 0: return 1; return (2 << n) - fn(0); # technically recursive

    – MooseBoys
    Oct 15 at 6:28






  • 3





    @Voo: Not Carl, but please list me everything that is contained in C:MyFolder

    – Flater
    Oct 16 at 11:11






  • 1





    @Voo: Dependency or not is irrelevant for an exercise which purely focuses on teaching the concept of recursion. I could make a basic mocked set of classes/methods that students could use. You're focusing on something that is completely besides the point of the exercise. Using file system navigation is a good example because students generally understand the inherently recurrent nature of folders and files (i.e. folders can be nested in each other nigh indefinitely)

    – Flater
    Oct 16 at 11:47







  • 1





    @Voo No I'm saying that you can teach recursion by showing a recursive data structure. I have no idea why you struggle grasping this.

    – Flater
    Oct 16 at 21:46












  • 4





    Just for the record, it should be vastly more efficient to do it like a normal person with a shift and subtract. Python integers are arbitrary width so 1 << n can't overflow. This seems to be an exercise in inventing a way to decompose (1<<n) - 1 into multiple steps, perhaps setting each bit one at a time like some answers show.

    – Peter Cordes
    Oct 14 at 23:57






  • 8





    def fn(n): if n == 0: return 1; return (2 << n) - fn(0); # technically recursive

    – MooseBoys
    Oct 15 at 6:28






  • 3





    @Voo: Not Carl, but please list me everything that is contained in C:MyFolder

    – Flater
    Oct 16 at 11:11






  • 1





    @Voo: Dependency or not is irrelevant for an exercise which purely focuses on teaching the concept of recursion. I could make a basic mocked set of classes/methods that students could use. You're focusing on something that is completely besides the point of the exercise. Using file system navigation is a good example because students generally understand the inherently recurrent nature of folders and files (i.e. folders can be nested in each other nigh indefinitely)

    – Flater
    Oct 16 at 11:47







  • 1





    @Voo No I'm saying that you can teach recursion by showing a recursive data structure. I have no idea why you struggle grasping this.

    – Flater
    Oct 16 at 21:46







4




4





Just for the record, it should be vastly more efficient to do it like a normal person with a shift and subtract. Python integers are arbitrary width so 1 << n can't overflow. This seems to be an exercise in inventing a way to decompose (1<<n) - 1 into multiple steps, perhaps setting each bit one at a time like some answers show.

– Peter Cordes
Oct 14 at 23:57





Just for the record, it should be vastly more efficient to do it like a normal person with a shift and subtract. Python integers are arbitrary width so 1 << n can't overflow. This seems to be an exercise in inventing a way to decompose (1<<n) - 1 into multiple steps, perhaps setting each bit one at a time like some answers show.

– Peter Cordes
Oct 14 at 23:57




8




8





def fn(n): if n == 0: return 1; return (2 << n) - fn(0); # technically recursive

– MooseBoys
Oct 15 at 6:28





def fn(n): if n == 0: return 1; return (2 << n) - fn(0); # technically recursive

– MooseBoys
Oct 15 at 6:28




3




3





@Voo: Not Carl, but please list me everything that is contained in C:MyFolder

– Flater
Oct 16 at 11:11





@Voo: Not Carl, but please list me everything that is contained in C:MyFolder

– Flater
Oct 16 at 11:11




1




1





@Voo: Dependency or not is irrelevant for an exercise which purely focuses on teaching the concept of recursion. I could make a basic mocked set of classes/methods that students could use. You're focusing on something that is completely besides the point of the exercise. Using file system navigation is a good example because students generally understand the inherently recurrent nature of folders and files (i.e. folders can be nested in each other nigh indefinitely)

– Flater
Oct 16 at 11:47






@Voo: Dependency or not is irrelevant for an exercise which purely focuses on teaching the concept of recursion. I could make a basic mocked set of classes/methods that students could use. You're focusing on something that is completely besides the point of the exercise. Using file system navigation is a good example because students generally understand the inherently recurrent nature of folders and files (i.e. folders can be nested in each other nigh indefinitely)

– Flater
Oct 16 at 11:47





1




1





@Voo No I'm saying that you can teach recursion by showing a recursive data structure. I have no idea why you struggle grasping this.

– Flater
Oct 16 at 21:46





@Voo No I'm saying that you can teach recursion by showing a recursive data structure. I have no idea why you struggle grasping this.

– Flater
Oct 16 at 21:46












7 Answers
7






active

oldest

votes


















50



















2**n -1 is also 1+2+4+...+2n-1 which can made into a single recursive function (without the second one to subtract 1 from the power of 2).



Hint: 1+2*(1+2*(...))



Solution below, don't look if you want to try the hint first.




This works if n is guaranteed to be greater than zero (as was actually promised in the problem statement):



def required_steps(n):
if n == 1: # changed because we need one less going down
return 1
return 1 + 2 * required_steps(n-1)


A more robust version would handle zero and negative values too:



def required_steps(n):
if n < 0:
raise ValueError("n must be non-negative")
if n == 0:
return 0
return 1 + 2 * required_steps(n-1)


(Adding a check for non-integers is left as an exercise.)






share|improve this answer























  • 4





    but required_steps(0) now causes infinite recursion

    – Thank you
    Oct 14 at 14:17







  • 7





    2^0 - 1 == 0. Add another if for that case.

    – h4z3
    Oct 14 at 14:18






  • 9





    @user633183 Yes, I know what a total function is. Do you? Because it won't ever be a total function. Nor are the other answers total functions. And yes, more code would be needed to make them total functions. - As I said, we have no domain. What should we assume our domain is? Even if it's just int, we don't know what to do when n<0. Calculate? Throw an error? Return 0? In this case, we only can do a partial function (define it for things we know what the result is).

    – h4z3
    Oct 14 at 14:39






  • 4





    The base case in the OP’s code is 0 and uses n - 1 for the subproblem. A domain of Natural Numbers seems like a good fit.

    – Thank you
    Oct 14 at 14:47






  • 4





    Thank you so much! In my humble opinion, this is the best solution for my specific problem. I didn't state possible values for n, really sorry! I know that is kinda important... the exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"

    – Kajice
    Oct 14 at 15:40


















35



















To solve a problem with a recursive approach you would have to find out how you can define the function with a given input in terms of the same function with a different input. In this case, since f(n) = 2 * f(n - 1) + 1, you can do:



def required_steps(n):
return n and 2 * required_steps(n - 1) + 1


so that:



for i in range(5):
print(required_steps(i))


outputs:



0
1
3
7
15





share|improve this answer



































    9



















    You can extract the really recursive part to another function



    def f(n):
    return required_steps(n) - 1


    Or you can set a flag and define just when to subtract



    def required_steps(n, sub=True):
    if n == 0: return 1
    return 2 * required_steps(n-1, False) - sub



    >>> print(required_steps(10))
    1023





    share|improve this answer

































      0



















      Using an additional parameter for the result, r -



      def required_steps (n = 0, r = 1):
      if n == 0:
      return r - 1
      else:
      return required_steps(n - 1, r * 2)

      for x in range(6):
      print(f"f(x) = required_steps(x)")

      # f(0) = 0
      # f(1) = 1
      # f(2) = 3
      # f(3) = 7
      # f(4) = 15
      # f(5) = 31


      You can also write it using bitwise left shift, << -



      def required_steps (n = 0, r = 1):
      if n == 0:
      return r - 1
      else:
      return required_steps(n - 1, r << 1)


      The output is the same






      share|improve this answer





















      • 2





        Unnecessary to involve bitwise operations for a simple multiplication exercise.. not readable at all. Also, no need for the else clause in either function

        – rafaelc
        Oct 14 at 14:56






      • 9





        Yep. r*2 is very readable, r << 1 is not readable at all

        – rafaelc
        Oct 14 at 15:55






      • 2





        Inventing a 2nd parameter just turns this into a loop that shifts left n times and then subtracts 1. Seems even less elegant then necessary, although the whole thing is an exercise in inefficiency vs. (1<<n) - 1.

        – Peter Cordes
        Oct 15 at 1:03






      • 1





        @PeterCordes: Moving the state into an accumulator parameter is the standard way of transforming a recursive call into a tail-recursive call. Now, unfortunately, Python doesn't support Proper Tail Calls, not even Proper Tail Recursion, but that doesn't mean that this isn't a useful technique to learn so that you can apply it in other languages that do implement Proper Tail Calls or at least Proper Tail Recursion.

        – Jörg W Mittag
        Oct 15 at 3:10







      • 1





        @JörgWMittag Yes, but in this case it's hard to disguise the fact that it would be more natural as a loop. Maybe it's just that I spend so much time on assembly language and performance, but writing a "loop" by using tail-recursion seems pointless in an imperative language when you could just write a loop. Or perhaps what bothers me about this answer is just the choice of how to decompose: into one-at-a-time shifts and then a final subtract as the basecase. Probably a combination of both.

        – Peter Cordes
        Oct 15 at 3:21


















      0



















      Have a placeholder to remember original value of n and then for the very first step i.e. n == N, return 2^n-1



      n = 10
      # constant to hold initial value of n
      N = n
      def required_steps(n, N):
      if n == 0:
      return 1
      elif n == N:
      return 2 * required_steps(n-1, N) - 1
      return 2 * required_steps(n-1, N)

      required_steps(n, N)





      share|improve this answer



































        -1



















        One way to get the offset of "-1" is to apply it in the return from the first function call using an argument with a default value, then explicitly set the offset argument to zero during the recursive calls.



        def required_steps(n, offset = -1):
        if n == 0:
        return 1
        return offset + 2 * required_steps(n-1,0)





        share|improve this answer

































          -1



















          On top of all the awesome answers given earlier, below will show its implementation with inner functions.



          def outer(n):
          k=n
          def p(n):
          if n==1:
          return 2
          if n==k:
          return 2*p(n-1)-1
          return 2*p(n-1)
          return p(n)

          n=5
          print(outer(n))


          Basically, it is assigning a global value of n to k and recursing through it with appropriate comparisons.






          share|improve this answer



























            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',
            autoActivateHeartbeat: false,
            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/4.0/"u003ecc by-sa 4.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%2f58378549%2fhow-to-write-2n-1-as-a-recursive-function%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown


























            7 Answers
            7






            active

            oldest

            votes








            7 Answers
            7






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            50



















            2**n -1 is also 1+2+4+...+2n-1 which can made into a single recursive function (without the second one to subtract 1 from the power of 2).



            Hint: 1+2*(1+2*(...))



            Solution below, don't look if you want to try the hint first.




            This works if n is guaranteed to be greater than zero (as was actually promised in the problem statement):



            def required_steps(n):
            if n == 1: # changed because we need one less going down
            return 1
            return 1 + 2 * required_steps(n-1)


            A more robust version would handle zero and negative values too:



            def required_steps(n):
            if n < 0:
            raise ValueError("n must be non-negative")
            if n == 0:
            return 0
            return 1 + 2 * required_steps(n-1)


            (Adding a check for non-integers is left as an exercise.)






            share|improve this answer























            • 4





              but required_steps(0) now causes infinite recursion

              – Thank you
              Oct 14 at 14:17







            • 7





              2^0 - 1 == 0. Add another if for that case.

              – h4z3
              Oct 14 at 14:18






            • 9





              @user633183 Yes, I know what a total function is. Do you? Because it won't ever be a total function. Nor are the other answers total functions. And yes, more code would be needed to make them total functions. - As I said, we have no domain. What should we assume our domain is? Even if it's just int, we don't know what to do when n<0. Calculate? Throw an error? Return 0? In this case, we only can do a partial function (define it for things we know what the result is).

              – h4z3
              Oct 14 at 14:39






            • 4





              The base case in the OP’s code is 0 and uses n - 1 for the subproblem. A domain of Natural Numbers seems like a good fit.

              – Thank you
              Oct 14 at 14:47






            • 4





              Thank you so much! In my humble opinion, this is the best solution for my specific problem. I didn't state possible values for n, really sorry! I know that is kinda important... the exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"

              – Kajice
              Oct 14 at 15:40















            50



















            2**n -1 is also 1+2+4+...+2n-1 which can made into a single recursive function (without the second one to subtract 1 from the power of 2).



            Hint: 1+2*(1+2*(...))



            Solution below, don't look if you want to try the hint first.




            This works if n is guaranteed to be greater than zero (as was actually promised in the problem statement):



            def required_steps(n):
            if n == 1: # changed because we need one less going down
            return 1
            return 1 + 2 * required_steps(n-1)


            A more robust version would handle zero and negative values too:



            def required_steps(n):
            if n < 0:
            raise ValueError("n must be non-negative")
            if n == 0:
            return 0
            return 1 + 2 * required_steps(n-1)


            (Adding a check for non-integers is left as an exercise.)






            share|improve this answer























            • 4





              but required_steps(0) now causes infinite recursion

              – Thank you
              Oct 14 at 14:17







            • 7





              2^0 - 1 == 0. Add another if for that case.

              – h4z3
              Oct 14 at 14:18






            • 9





              @user633183 Yes, I know what a total function is. Do you? Because it won't ever be a total function. Nor are the other answers total functions. And yes, more code would be needed to make them total functions. - As I said, we have no domain. What should we assume our domain is? Even if it's just int, we don't know what to do when n<0. Calculate? Throw an error? Return 0? In this case, we only can do a partial function (define it for things we know what the result is).

              – h4z3
              Oct 14 at 14:39






            • 4





              The base case in the OP’s code is 0 and uses n - 1 for the subproblem. A domain of Natural Numbers seems like a good fit.

              – Thank you
              Oct 14 at 14:47






            • 4





              Thank you so much! In my humble opinion, this is the best solution for my specific problem. I didn't state possible values for n, really sorry! I know that is kinda important... the exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"

              – Kajice
              Oct 14 at 15:40













            50














            50










            50










            2**n -1 is also 1+2+4+...+2n-1 which can made into a single recursive function (without the second one to subtract 1 from the power of 2).



            Hint: 1+2*(1+2*(...))



            Solution below, don't look if you want to try the hint first.




            This works if n is guaranteed to be greater than zero (as was actually promised in the problem statement):



            def required_steps(n):
            if n == 1: # changed because we need one less going down
            return 1
            return 1 + 2 * required_steps(n-1)


            A more robust version would handle zero and negative values too:



            def required_steps(n):
            if n < 0:
            raise ValueError("n must be non-negative")
            if n == 0:
            return 0
            return 1 + 2 * required_steps(n-1)


            (Adding a check for non-integers is left as an exercise.)






            share|improve this answer
















            2**n -1 is also 1+2+4+...+2n-1 which can made into a single recursive function (without the second one to subtract 1 from the power of 2).



            Hint: 1+2*(1+2*(...))



            Solution below, don't look if you want to try the hint first.




            This works if n is guaranteed to be greater than zero (as was actually promised in the problem statement):



            def required_steps(n):
            if n == 1: # changed because we need one less going down
            return 1
            return 1 + 2 * required_steps(n-1)


            A more robust version would handle zero and negative values too:



            def required_steps(n):
            if n < 0:
            raise ValueError("n must be non-negative")
            if n == 0:
            return 0
            return 1 + 2 * required_steps(n-1)


            (Adding a check for non-integers is left as an exercise.)







            share|improve this answer















            share|improve this answer




            share|improve this answer








            edited Oct 15 at 18:53









            ilkkachu

            3,7735 silver badges20 bronze badges




            3,7735 silver badges20 bronze badges










            answered Oct 14 at 14:16









            h4z3h4z3

            2,3381 gold badge3 silver badges17 bronze badges




            2,3381 gold badge3 silver badges17 bronze badges










            • 4





              but required_steps(0) now causes infinite recursion

              – Thank you
              Oct 14 at 14:17







            • 7





              2^0 - 1 == 0. Add another if for that case.

              – h4z3
              Oct 14 at 14:18






            • 9





              @user633183 Yes, I know what a total function is. Do you? Because it won't ever be a total function. Nor are the other answers total functions. And yes, more code would be needed to make them total functions. - As I said, we have no domain. What should we assume our domain is? Even if it's just int, we don't know what to do when n<0. Calculate? Throw an error? Return 0? In this case, we only can do a partial function (define it for things we know what the result is).

              – h4z3
              Oct 14 at 14:39






            • 4





              The base case in the OP’s code is 0 and uses n - 1 for the subproblem. A domain of Natural Numbers seems like a good fit.

              – Thank you
              Oct 14 at 14:47






            • 4





              Thank you so much! In my humble opinion, this is the best solution for my specific problem. I didn't state possible values for n, really sorry! I know that is kinda important... the exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"

              – Kajice
              Oct 14 at 15:40












            • 4





              but required_steps(0) now causes infinite recursion

              – Thank you
              Oct 14 at 14:17







            • 7





              2^0 - 1 == 0. Add another if for that case.

              – h4z3
              Oct 14 at 14:18






            • 9





              @user633183 Yes, I know what a total function is. Do you? Because it won't ever be a total function. Nor are the other answers total functions. And yes, more code would be needed to make them total functions. - As I said, we have no domain. What should we assume our domain is? Even if it's just int, we don't know what to do when n<0. Calculate? Throw an error? Return 0? In this case, we only can do a partial function (define it for things we know what the result is).

              – h4z3
              Oct 14 at 14:39






            • 4





              The base case in the OP’s code is 0 and uses n - 1 for the subproblem. A domain of Natural Numbers seems like a good fit.

              – Thank you
              Oct 14 at 14:47






            • 4





              Thank you so much! In my humble opinion, this is the best solution for my specific problem. I didn't state possible values for n, really sorry! I know that is kinda important... the exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"

              – Kajice
              Oct 14 at 15:40







            4




            4





            but required_steps(0) now causes infinite recursion

            – Thank you
            Oct 14 at 14:17






            but required_steps(0) now causes infinite recursion

            – Thank you
            Oct 14 at 14:17





            7




            7





            2^0 - 1 == 0. Add another if for that case.

            – h4z3
            Oct 14 at 14:18





            2^0 - 1 == 0. Add another if for that case.

            – h4z3
            Oct 14 at 14:18




            9




            9





            @user633183 Yes, I know what a total function is. Do you? Because it won't ever be a total function. Nor are the other answers total functions. And yes, more code would be needed to make them total functions. - As I said, we have no domain. What should we assume our domain is? Even if it's just int, we don't know what to do when n<0. Calculate? Throw an error? Return 0? In this case, we only can do a partial function (define it for things we know what the result is).

            – h4z3
            Oct 14 at 14:39





            @user633183 Yes, I know what a total function is. Do you? Because it won't ever be a total function. Nor are the other answers total functions. And yes, more code would be needed to make them total functions. - As I said, we have no domain. What should we assume our domain is? Even if it's just int, we don't know what to do when n<0. Calculate? Throw an error? Return 0? In this case, we only can do a partial function (define it for things we know what the result is).

            – h4z3
            Oct 14 at 14:39




            4




            4





            The base case in the OP’s code is 0 and uses n - 1 for the subproblem. A domain of Natural Numbers seems like a good fit.

            – Thank you
            Oct 14 at 14:47





            The base case in the OP’s code is 0 and uses n - 1 for the subproblem. A domain of Natural Numbers seems like a good fit.

            – Thank you
            Oct 14 at 14:47




            4




            4





            Thank you so much! In my humble opinion, this is the best solution for my specific problem. I didn't state possible values for n, really sorry! I know that is kinda important... the exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"

            – Kajice
            Oct 14 at 15:40





            Thank you so much! In my humble opinion, this is the best solution for my specific problem. I didn't state possible values for n, really sorry! I know that is kinda important... the exercise states: "You can assume that the parameter n is always a positive integer and greater than 0"

            – Kajice
            Oct 14 at 15:40













            35



















            To solve a problem with a recursive approach you would have to find out how you can define the function with a given input in terms of the same function with a different input. In this case, since f(n) = 2 * f(n - 1) + 1, you can do:



            def required_steps(n):
            return n and 2 * required_steps(n - 1) + 1


            so that:



            for i in range(5):
            print(required_steps(i))


            outputs:



            0
            1
            3
            7
            15





            share|improve this answer
































              35



















              To solve a problem with a recursive approach you would have to find out how you can define the function with a given input in terms of the same function with a different input. In this case, since f(n) = 2 * f(n - 1) + 1, you can do:



              def required_steps(n):
              return n and 2 * required_steps(n - 1) + 1


              so that:



              for i in range(5):
              print(required_steps(i))


              outputs:



              0
              1
              3
              7
              15





              share|improve this answer






























                35














                35










                35










                To solve a problem with a recursive approach you would have to find out how you can define the function with a given input in terms of the same function with a different input. In this case, since f(n) = 2 * f(n - 1) + 1, you can do:



                def required_steps(n):
                return n and 2 * required_steps(n - 1) + 1


                so that:



                for i in range(5):
                print(required_steps(i))


                outputs:



                0
                1
                3
                7
                15





                share|improve this answer
















                To solve a problem with a recursive approach you would have to find out how you can define the function with a given input in terms of the same function with a different input. In this case, since f(n) = 2 * f(n - 1) + 1, you can do:



                def required_steps(n):
                return n and 2 * required_steps(n - 1) + 1


                so that:



                for i in range(5):
                print(required_steps(i))


                outputs:



                0
                1
                3
                7
                15






                share|improve this answer















                share|improve this answer




                share|improve this answer








                edited Oct 15 at 17:43

























                answered Oct 14 at 14:20









                blhsingblhsing

                55k6 gold badges22 silver badges52 bronze badges




                55k6 gold badges22 silver badges52 bronze badges
























                    9



















                    You can extract the really recursive part to another function



                    def f(n):
                    return required_steps(n) - 1


                    Or you can set a flag and define just when to subtract



                    def required_steps(n, sub=True):
                    if n == 0: return 1
                    return 2 * required_steps(n-1, False) - sub



                    >>> print(required_steps(10))
                    1023





                    share|improve this answer






























                      9



















                      You can extract the really recursive part to another function



                      def f(n):
                      return required_steps(n) - 1


                      Or you can set a flag and define just when to subtract



                      def required_steps(n, sub=True):
                      if n == 0: return 1
                      return 2 * required_steps(n-1, False) - sub



                      >>> print(required_steps(10))
                      1023





                      share|improve this answer




























                        9














                        9










                        9










                        You can extract the really recursive part to another function



                        def f(n):
                        return required_steps(n) - 1


                        Or you can set a flag and define just when to subtract



                        def required_steps(n, sub=True):
                        if n == 0: return 1
                        return 2 * required_steps(n-1, False) - sub



                        >>> print(required_steps(10))
                        1023





                        share|improve this answer














                        You can extract the really recursive part to another function



                        def f(n):
                        return required_steps(n) - 1


                        Or you can set a flag and define just when to subtract



                        def required_steps(n, sub=True):
                        if n == 0: return 1
                        return 2 * required_steps(n-1, False) - sub



                        >>> print(required_steps(10))
                        1023






                        share|improve this answer













                        share|improve this answer




                        share|improve this answer










                        answered Oct 14 at 14:14









                        rafaelcrafaelc

                        38.6k11 gold badges35 silver badges57 bronze badges




                        38.6k11 gold badges35 silver badges57 bronze badges
























                            0



















                            Using an additional parameter for the result, r -



                            def required_steps (n = 0, r = 1):
                            if n == 0:
                            return r - 1
                            else:
                            return required_steps(n - 1, r * 2)

                            for x in range(6):
                            print(f"f(x) = required_steps(x)")

                            # f(0) = 0
                            # f(1) = 1
                            # f(2) = 3
                            # f(3) = 7
                            # f(4) = 15
                            # f(5) = 31


                            You can also write it using bitwise left shift, << -



                            def required_steps (n = 0, r = 1):
                            if n == 0:
                            return r - 1
                            else:
                            return required_steps(n - 1, r << 1)


                            The output is the same






                            share|improve this answer





















                            • 2





                              Unnecessary to involve bitwise operations for a simple multiplication exercise.. not readable at all. Also, no need for the else clause in either function

                              – rafaelc
                              Oct 14 at 14:56






                            • 9





                              Yep. r*2 is very readable, r << 1 is not readable at all

                              – rafaelc
                              Oct 14 at 15:55






                            • 2





                              Inventing a 2nd parameter just turns this into a loop that shifts left n times and then subtracts 1. Seems even less elegant then necessary, although the whole thing is an exercise in inefficiency vs. (1<<n) - 1.

                              – Peter Cordes
                              Oct 15 at 1:03






                            • 1





                              @PeterCordes: Moving the state into an accumulator parameter is the standard way of transforming a recursive call into a tail-recursive call. Now, unfortunately, Python doesn't support Proper Tail Calls, not even Proper Tail Recursion, but that doesn't mean that this isn't a useful technique to learn so that you can apply it in other languages that do implement Proper Tail Calls or at least Proper Tail Recursion.

                              – Jörg W Mittag
                              Oct 15 at 3:10







                            • 1





                              @JörgWMittag Yes, but in this case it's hard to disguise the fact that it would be more natural as a loop. Maybe it's just that I spend so much time on assembly language and performance, but writing a "loop" by using tail-recursion seems pointless in an imperative language when you could just write a loop. Or perhaps what bothers me about this answer is just the choice of how to decompose: into one-at-a-time shifts and then a final subtract as the basecase. Probably a combination of both.

                              – Peter Cordes
                              Oct 15 at 3:21















                            0



















                            Using an additional parameter for the result, r -



                            def required_steps (n = 0, r = 1):
                            if n == 0:
                            return r - 1
                            else:
                            return required_steps(n - 1, r * 2)

                            for x in range(6):
                            print(f"f(x) = required_steps(x)")

                            # f(0) = 0
                            # f(1) = 1
                            # f(2) = 3
                            # f(3) = 7
                            # f(4) = 15
                            # f(5) = 31


                            You can also write it using bitwise left shift, << -



                            def required_steps (n = 0, r = 1):
                            if n == 0:
                            return r - 1
                            else:
                            return required_steps(n - 1, r << 1)


                            The output is the same






                            share|improve this answer





















                            • 2





                              Unnecessary to involve bitwise operations for a simple multiplication exercise.. not readable at all. Also, no need for the else clause in either function

                              – rafaelc
                              Oct 14 at 14:56






                            • 9





                              Yep. r*2 is very readable, r << 1 is not readable at all

                              – rafaelc
                              Oct 14 at 15:55






                            • 2





                              Inventing a 2nd parameter just turns this into a loop that shifts left n times and then subtracts 1. Seems even less elegant then necessary, although the whole thing is an exercise in inefficiency vs. (1<<n) - 1.

                              – Peter Cordes
                              Oct 15 at 1:03






                            • 1





                              @PeterCordes: Moving the state into an accumulator parameter is the standard way of transforming a recursive call into a tail-recursive call. Now, unfortunately, Python doesn't support Proper Tail Calls, not even Proper Tail Recursion, but that doesn't mean that this isn't a useful technique to learn so that you can apply it in other languages that do implement Proper Tail Calls or at least Proper Tail Recursion.

                              – Jörg W Mittag
                              Oct 15 at 3:10







                            • 1





                              @JörgWMittag Yes, but in this case it's hard to disguise the fact that it would be more natural as a loop. Maybe it's just that I spend so much time on assembly language and performance, but writing a "loop" by using tail-recursion seems pointless in an imperative language when you could just write a loop. Or perhaps what bothers me about this answer is just the choice of how to decompose: into one-at-a-time shifts and then a final subtract as the basecase. Probably a combination of both.

                              – Peter Cordes
                              Oct 15 at 3:21













                            0














                            0










                            0










                            Using an additional parameter for the result, r -



                            def required_steps (n = 0, r = 1):
                            if n == 0:
                            return r - 1
                            else:
                            return required_steps(n - 1, r * 2)

                            for x in range(6):
                            print(f"f(x) = required_steps(x)")

                            # f(0) = 0
                            # f(1) = 1
                            # f(2) = 3
                            # f(3) = 7
                            # f(4) = 15
                            # f(5) = 31


                            You can also write it using bitwise left shift, << -



                            def required_steps (n = 0, r = 1):
                            if n == 0:
                            return r - 1
                            else:
                            return required_steps(n - 1, r << 1)


                            The output is the same






                            share|improve this answer














                            Using an additional parameter for the result, r -



                            def required_steps (n = 0, r = 1):
                            if n == 0:
                            return r - 1
                            else:
                            return required_steps(n - 1, r * 2)

                            for x in range(6):
                            print(f"f(x) = required_steps(x)")

                            # f(0) = 0
                            # f(1) = 1
                            # f(2) = 3
                            # f(3) = 7
                            # f(4) = 15
                            # f(5) = 31


                            You can also write it using bitwise left shift, << -



                            def required_steps (n = 0, r = 1):
                            if n == 0:
                            return r - 1
                            else:
                            return required_steps(n - 1, r << 1)


                            The output is the same







                            share|improve this answer













                            share|improve this answer




                            share|improve this answer










                            answered Oct 14 at 14:21









                            Thank youThank you

                            80.2k22 gold badges153 silver badges195 bronze badges




                            80.2k22 gold badges153 silver badges195 bronze badges










                            • 2





                              Unnecessary to involve bitwise operations for a simple multiplication exercise.. not readable at all. Also, no need for the else clause in either function

                              – rafaelc
                              Oct 14 at 14:56






                            • 9





                              Yep. r*2 is very readable, r << 1 is not readable at all

                              – rafaelc
                              Oct 14 at 15:55






                            • 2





                              Inventing a 2nd parameter just turns this into a loop that shifts left n times and then subtracts 1. Seems even less elegant then necessary, although the whole thing is an exercise in inefficiency vs. (1<<n) - 1.

                              – Peter Cordes
                              Oct 15 at 1:03






                            • 1





                              @PeterCordes: Moving the state into an accumulator parameter is the standard way of transforming a recursive call into a tail-recursive call. Now, unfortunately, Python doesn't support Proper Tail Calls, not even Proper Tail Recursion, but that doesn't mean that this isn't a useful technique to learn so that you can apply it in other languages that do implement Proper Tail Calls or at least Proper Tail Recursion.

                              – Jörg W Mittag
                              Oct 15 at 3:10







                            • 1





                              @JörgWMittag Yes, but in this case it's hard to disguise the fact that it would be more natural as a loop. Maybe it's just that I spend so much time on assembly language and performance, but writing a "loop" by using tail-recursion seems pointless in an imperative language when you could just write a loop. Or perhaps what bothers me about this answer is just the choice of how to decompose: into one-at-a-time shifts and then a final subtract as the basecase. Probably a combination of both.

                              – Peter Cordes
                              Oct 15 at 3:21












                            • 2





                              Unnecessary to involve bitwise operations for a simple multiplication exercise.. not readable at all. Also, no need for the else clause in either function

                              – rafaelc
                              Oct 14 at 14:56






                            • 9





                              Yep. r*2 is very readable, r << 1 is not readable at all

                              – rafaelc
                              Oct 14 at 15:55






                            • 2





                              Inventing a 2nd parameter just turns this into a loop that shifts left n times and then subtracts 1. Seems even less elegant then necessary, although the whole thing is an exercise in inefficiency vs. (1<<n) - 1.

                              – Peter Cordes
                              Oct 15 at 1:03






                            • 1





                              @PeterCordes: Moving the state into an accumulator parameter is the standard way of transforming a recursive call into a tail-recursive call. Now, unfortunately, Python doesn't support Proper Tail Calls, not even Proper Tail Recursion, but that doesn't mean that this isn't a useful technique to learn so that you can apply it in other languages that do implement Proper Tail Calls or at least Proper Tail Recursion.

                              – Jörg W Mittag
                              Oct 15 at 3:10







                            • 1





                              @JörgWMittag Yes, but in this case it's hard to disguise the fact that it would be more natural as a loop. Maybe it's just that I spend so much time on assembly language and performance, but writing a "loop" by using tail-recursion seems pointless in an imperative language when you could just write a loop. Or perhaps what bothers me about this answer is just the choice of how to decompose: into one-at-a-time shifts and then a final subtract as the basecase. Probably a combination of both.

                              – Peter Cordes
                              Oct 15 at 3:21







                            2




                            2





                            Unnecessary to involve bitwise operations for a simple multiplication exercise.. not readable at all. Also, no need for the else clause in either function

                            – rafaelc
                            Oct 14 at 14:56





                            Unnecessary to involve bitwise operations for a simple multiplication exercise.. not readable at all. Also, no need for the else clause in either function

                            – rafaelc
                            Oct 14 at 14:56




                            9




                            9





                            Yep. r*2 is very readable, r << 1 is not readable at all

                            – rafaelc
                            Oct 14 at 15:55





                            Yep. r*2 is very readable, r << 1 is not readable at all

                            – rafaelc
                            Oct 14 at 15:55




                            2




                            2





                            Inventing a 2nd parameter just turns this into a loop that shifts left n times and then subtracts 1. Seems even less elegant then necessary, although the whole thing is an exercise in inefficiency vs. (1<<n) - 1.

                            – Peter Cordes
                            Oct 15 at 1:03





                            Inventing a 2nd parameter just turns this into a loop that shifts left n times and then subtracts 1. Seems even less elegant then necessary, although the whole thing is an exercise in inefficiency vs. (1<<n) - 1.

                            – Peter Cordes
                            Oct 15 at 1:03




                            1




                            1





                            @PeterCordes: Moving the state into an accumulator parameter is the standard way of transforming a recursive call into a tail-recursive call. Now, unfortunately, Python doesn't support Proper Tail Calls, not even Proper Tail Recursion, but that doesn't mean that this isn't a useful technique to learn so that you can apply it in other languages that do implement Proper Tail Calls or at least Proper Tail Recursion.

                            – Jörg W Mittag
                            Oct 15 at 3:10






                            @PeterCordes: Moving the state into an accumulator parameter is the standard way of transforming a recursive call into a tail-recursive call. Now, unfortunately, Python doesn't support Proper Tail Calls, not even Proper Tail Recursion, but that doesn't mean that this isn't a useful technique to learn so that you can apply it in other languages that do implement Proper Tail Calls or at least Proper Tail Recursion.

                            – Jörg W Mittag
                            Oct 15 at 3:10





                            1




                            1





                            @JörgWMittag Yes, but in this case it's hard to disguise the fact that it would be more natural as a loop. Maybe it's just that I spend so much time on assembly language and performance, but writing a "loop" by using tail-recursion seems pointless in an imperative language when you could just write a loop. Or perhaps what bothers me about this answer is just the choice of how to decompose: into one-at-a-time shifts and then a final subtract as the basecase. Probably a combination of both.

                            – Peter Cordes
                            Oct 15 at 3:21





                            @JörgWMittag Yes, but in this case it's hard to disguise the fact that it would be more natural as a loop. Maybe it's just that I spend so much time on assembly language and performance, but writing a "loop" by using tail-recursion seems pointless in an imperative language when you could just write a loop. Or perhaps what bothers me about this answer is just the choice of how to decompose: into one-at-a-time shifts and then a final subtract as the basecase. Probably a combination of both.

                            – Peter Cordes
                            Oct 15 at 3:21











                            0



















                            Have a placeholder to remember original value of n and then for the very first step i.e. n == N, return 2^n-1



                            n = 10
                            # constant to hold initial value of n
                            N = n
                            def required_steps(n, N):
                            if n == 0:
                            return 1
                            elif n == N:
                            return 2 * required_steps(n-1, N) - 1
                            return 2 * required_steps(n-1, N)

                            required_steps(n, N)





                            share|improve this answer
































                              0



















                              Have a placeholder to remember original value of n and then for the very first step i.e. n == N, return 2^n-1



                              n = 10
                              # constant to hold initial value of n
                              N = n
                              def required_steps(n, N):
                              if n == 0:
                              return 1
                              elif n == N:
                              return 2 * required_steps(n-1, N) - 1
                              return 2 * required_steps(n-1, N)

                              required_steps(n, N)





                              share|improve this answer






























                                0














                                0










                                0










                                Have a placeholder to remember original value of n and then for the very first step i.e. n == N, return 2^n-1



                                n = 10
                                # constant to hold initial value of n
                                N = n
                                def required_steps(n, N):
                                if n == 0:
                                return 1
                                elif n == N:
                                return 2 * required_steps(n-1, N) - 1
                                return 2 * required_steps(n-1, N)

                                required_steps(n, N)





                                share|improve this answer
















                                Have a placeholder to remember original value of n and then for the very first step i.e. n == N, return 2^n-1



                                n = 10
                                # constant to hold initial value of n
                                N = n
                                def required_steps(n, N):
                                if n == 0:
                                return 1
                                elif n == N:
                                return 2 * required_steps(n-1, N) - 1
                                return 2 * required_steps(n-1, N)

                                required_steps(n, N)






                                share|improve this answer















                                share|improve this answer




                                share|improve this answer








                                edited Oct 15 at 9:01

























                                answered Oct 14 at 14:15









                                eMadeMad

                                6383 gold badges10 silver badges26 bronze badges




                                6383 gold badges10 silver badges26 bronze badges
























                                    -1



















                                    One way to get the offset of "-1" is to apply it in the return from the first function call using an argument with a default value, then explicitly set the offset argument to zero during the recursive calls.



                                    def required_steps(n, offset = -1):
                                    if n == 0:
                                    return 1
                                    return offset + 2 * required_steps(n-1,0)





                                    share|improve this answer






























                                      -1



















                                      One way to get the offset of "-1" is to apply it in the return from the first function call using an argument with a default value, then explicitly set the offset argument to zero during the recursive calls.



                                      def required_steps(n, offset = -1):
                                      if n == 0:
                                      return 1
                                      return offset + 2 * required_steps(n-1,0)





                                      share|improve this answer




























                                        -1














                                        -1










                                        -1










                                        One way to get the offset of "-1" is to apply it in the return from the first function call using an argument with a default value, then explicitly set the offset argument to zero during the recursive calls.



                                        def required_steps(n, offset = -1):
                                        if n == 0:
                                        return 1
                                        return offset + 2 * required_steps(n-1,0)





                                        share|improve this answer














                                        One way to get the offset of "-1" is to apply it in the return from the first function call using an argument with a default value, then explicitly set the offset argument to zero during the recursive calls.



                                        def required_steps(n, offset = -1):
                                        if n == 0:
                                        return 1
                                        return offset + 2 * required_steps(n-1,0)






                                        share|improve this answer













                                        share|improve this answer




                                        share|improve this answer










                                        answered Oct 15 at 5:23









                                        Eric TowersEric Towers

                                        3,7171 gold badge11 silver badges15 bronze badges




                                        3,7171 gold badge11 silver badges15 bronze badges
























                                            -1



















                                            On top of all the awesome answers given earlier, below will show its implementation with inner functions.



                                            def outer(n):
                                            k=n
                                            def p(n):
                                            if n==1:
                                            return 2
                                            if n==k:
                                            return 2*p(n-1)-1
                                            return 2*p(n-1)
                                            return p(n)

                                            n=5
                                            print(outer(n))


                                            Basically, it is assigning a global value of n to k and recursing through it with appropriate comparisons.






                                            share|improve this answer






























                                              -1



















                                              On top of all the awesome answers given earlier, below will show its implementation with inner functions.



                                              def outer(n):
                                              k=n
                                              def p(n):
                                              if n==1:
                                              return 2
                                              if n==k:
                                              return 2*p(n-1)-1
                                              return 2*p(n-1)
                                              return p(n)

                                              n=5
                                              print(outer(n))


                                              Basically, it is assigning a global value of n to k and recursing through it with appropriate comparisons.






                                              share|improve this answer




























                                                -1














                                                -1










                                                -1










                                                On top of all the awesome answers given earlier, below will show its implementation with inner functions.



                                                def outer(n):
                                                k=n
                                                def p(n):
                                                if n==1:
                                                return 2
                                                if n==k:
                                                return 2*p(n-1)-1
                                                return 2*p(n-1)
                                                return p(n)

                                                n=5
                                                print(outer(n))


                                                Basically, it is assigning a global value of n to k and recursing through it with appropriate comparisons.






                                                share|improve this answer














                                                On top of all the awesome answers given earlier, below will show its implementation with inner functions.



                                                def outer(n):
                                                k=n
                                                def p(n):
                                                if n==1:
                                                return 2
                                                if n==k:
                                                return 2*p(n-1)-1
                                                return 2*p(n-1)
                                                return p(n)

                                                n=5
                                                print(outer(n))


                                                Basically, it is assigning a global value of n to k and recursing through it with appropriate comparisons.







                                                share|improve this answer













                                                share|improve this answer




                                                share|improve this answer










                                                answered Oct 15 at 12:50









                                                Your IDEYour IDE

                                                1018 bronze badges




                                                1018 bronze badges































                                                    draft saved

                                                    draft discarded















































                                                    Thanks for contributing an answer to Stack Overflow!


                                                    • Please be sure to answer the question. Provide details and share your research!

                                                    But avoid


                                                    • Asking for help, clarification, or responding to other answers.

                                                    • Making statements based on opinion; back them up with references or personal experience.

                                                    To learn more, see our tips on writing great answers.




                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f58378549%2fhow-to-write-2n-1-as-a-recursive-function%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest















                                                    Required, but never shown





















































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown

































                                                    Required, but never shown














                                                    Required, but never shown












                                                    Required, but never shown







                                                    Required, but never shown









                                                    Popular posts from this blog

                                                    Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                                                    Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                                                    Tom Holland Mục lục Đầu đời và giáo dục | Sự nghiệp | Cuộc sống cá nhân | Phim tham gia | Giải thưởng và đề cử | Chú thích | Liên kết ngoài | Trình đơn chuyển hướngProfile“Person Details for Thomas Stanley Holland, "England and Wales Birth Registration Index, 1837-2008" — FamilySearch.org”"Meet Tom Holland... the 16-year-old star of The Impossible""Schoolboy actor Tom Holland finds himself in Oscar contention for role in tsunami drama"“Naomi Watts on the Prince William and Harry's reaction to her film about the late Princess Diana”lưu trữ"Holland and Pflueger Are West End's Two New 'Billy Elliots'""I'm so envious of my son, the movie star! British writer Dominic Holland's spent 20 years trying to crack Hollywood - but he's been beaten to it by a very unlikely rival"“Richard and Margaret Povey of Jersey, Channel Islands, UK: Information about Thomas Stanley Holland”"Tom Holland to play Billy Elliot""New Billy Elliot leaving the garage"Billy Elliot the Musical - Tom Holland - Billy"A Tale of four Billys: Tom Holland""The Feel Good Factor""Thames Christian College schoolboys join Myleene Klass for The Feelgood Factor""Government launches £600,000 arts bursaries pilot""BILLY's Chapman, Holland, Gardner & Jackson-Keen Visit Prime Minister""Elton John 'blown away' by Billy Elliot fifth birthday" (video with John's interview and fragments of Holland's performance)"First News interviews Arrietty's Tom Holland"“33rd Critics' Circle Film Awards winners”“National Board of Review Current Awards”Bản gốc"Ron Howard Whaling Tale 'In The Heart Of The Sea' Casts Tom Holland"“'Spider-Man' Finds Tom Holland to Star as New Web-Slinger”lưu trữ“Captain America: Civil War (2016)”“Film Review: ‘Captain America: Civil War’”lưu trữ“‘Captain America: Civil War’ review: Choose your own avenger”lưu trữ“The Lost City of Z reviews”“Sony Pictures and Marvel Studios Find Their 'Spider-Man' Star and Director”“‘Mary Magdalene’, ‘Current War’ & ‘Wind River’ Get 2017 Release Dates From Weinstein”“Lionsgate Unleashing Daisy Ridley & Tom Holland Starrer ‘Chaos Walking’ In Cannes”“PTA's 'Master' Leads Chicago Film Critics Nominations, UPDATED: Houston and Indiana Critics Nominations”“Nominaciones Goya 2013 Telecinco Cinema – ENG”“Jameson Empire Film Awards: Martin Freeman wins best actor for performance in The Hobbit”“34th Annual Young Artist Awards”Bản gốc“Teen Choice Awards 2016—Captain America: Civil War Leads Second Wave of Nominations”“BAFTA Film Award Nominations: ‘La La Land’ Leads Race”“Saturn Awards Nominations 2017: 'Rogue One,' 'Walking Dead' Lead”Tom HollandTom HollandTom HollandTom Hollandmedia.gettyimages.comWorldCat Identities300279794no20130442900000 0004 0355 42791085670554170004732cb16706349t(data)XX5557367