The inverse Collatz Conjecture

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












I think the Collatz Conjecture is already well-known. But what if we invert the rules?



Start with an integer n >= 1.



Repeat the following steps:



If n is even, multiply it by 3 and add 1.



If n is odd, subtract 1 and divide it by 2.



Stop when it reaches 0



Print the iterated numbers.



Test cases:



 1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...


Rules:



  • This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.


  • I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.


  • You need to print the initial input as well.


  • The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.


  • The smallest code wins.










share|improve this question



















  • 2




    As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
    – caird coinheringaahing
    1 hour ago










  • Thank you @cairdcoinheringaahing. I didn't know about this page.
    – Eduardo Hoefel
    38 mins ago














up vote
2
down vote

favorite












I think the Collatz Conjecture is already well-known. But what if we invert the rules?



Start with an integer n >= 1.



Repeat the following steps:



If n is even, multiply it by 3 and add 1.



If n is odd, subtract 1 and divide it by 2.



Stop when it reaches 0



Print the iterated numbers.



Test cases:



 1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...


Rules:



  • This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.


  • I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.


  • You need to print the initial input as well.


  • The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.


  • The smallest code wins.










share|improve this question



















  • 2




    As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
    – caird coinheringaahing
    1 hour ago










  • Thank you @cairdcoinheringaahing. I didn't know about this page.
    – Eduardo Hoefel
    38 mins ago












up vote
2
down vote

favorite









up vote
2
down vote

favorite











I think the Collatz Conjecture is already well-known. But what if we invert the rules?



Start with an integer n >= 1.



Repeat the following steps:



If n is even, multiply it by 3 and add 1.



If n is odd, subtract 1 and divide it by 2.



Stop when it reaches 0



Print the iterated numbers.



Test cases:



 1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...


Rules:



  • This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.


  • I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.


  • You need to print the initial input as well.


  • The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.


  • The smallest code wins.










share|improve this question















I think the Collatz Conjecture is already well-known. But what if we invert the rules?



Start with an integer n >= 1.



Repeat the following steps:



If n is even, multiply it by 3 and add 1.



If n is odd, subtract 1 and divide it by 2.



Stop when it reaches 0



Print the iterated numbers.



Test cases:



 1 => 1, 0
2 => 2, 7, 3, 1, 0
3 => 3, 1, 0
10 => 10, 31, 15, 7, 3...
14 => 14, 43, 21, 10, ...


Rules:



  • This sequence does not work for a lot of numbers because it enters in an infinite loop. You do not need to handle those cases. Only printing the test cases above is enough.


  • I suggested to subtract 1 and divide by two to give a valid integer to continue, but it is not required to be computed that way. You may divide by 2 and cast to integer or whatever other methods that will give the expected output.


  • You need to print the initial input as well.


  • The output does not need to be formatted as the test cases. It was just a suggestion. However, the iterated order must be respected.


  • The smallest code wins.







code-golf math integer






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 36 mins ago

























asked 1 hour ago









Eduardo Hoefel

415




415







  • 2




    As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
    – caird coinheringaahing
    1 hour ago










  • Thank you @cairdcoinheringaahing. I didn't know about this page.
    – Eduardo Hoefel
    38 mins ago












  • 2




    As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
    – caird coinheringaahing
    1 hour ago










  • Thank you @cairdcoinheringaahing. I didn't know about this page.
    – Eduardo Hoefel
    38 mins ago







2




2




As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing
1 hour ago




As this is your third question in as many hours, I'd recommend that you check out the Sandbox, the place where we usually post question drafts for feedback, and to make sure they aren't duplicates.
– caird coinheringaahing
1 hour ago












Thank you @cairdcoinheringaahing. I didn't know about this page.
– Eduardo Hoefel
38 mins ago




Thank you @cairdcoinheringaahing. I didn't know about this page.
– Eduardo Hoefel
38 mins ago










11 Answers
11






active

oldest

votes

















up vote
0
down vote














Jelly, 9 bytes



:++‘ƊḂ?Ƭ2


Try it online!






share|improve this answer



























    up vote
    0
    down vote













    JavaScript (ES6), 31 bytes





    f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)


    Try it online!



    Or 30 bytes in reverse order.






    share|improve this answer





























      up vote
      0
      down vote














      Rust, 65 bytes





      fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);


      Try it online!






      share|improve this answer



























        up vote
        0
        down vote














        Pyth, 12 bytes



        .u?%N2/N2h*3


        Try it here as a test suite!






        share|improve this answer



























          up vote
          0
          down vote














          JAEL, 12 chars, 24 bytes



          ![⍩î?ûḄ|õÀ⸵ẘ


          I would post the TIO link but it's not working as expected there.



          Right now, the input must be prepended to the code, like:



          14![⍩î?ûḄ|õÀ⸵ẘ





          share|improve this answer



























            up vote
            0
            down vote













            perl -Minteger -nlE, 39 bytes



            say;$_=$_%2?$_/2:3*$_+1 and redosay 0





            share|improve this answer



























              up vote
              0
              down vote














              Clean, 53 bytes



              import StdEnv
              $0=[0]
              $n=[n: $if(isOdd n)(n/2)(n*3+1)]


              Try it online!






              share|improve this answer



























                up vote
                0
                down vote














                Add++, 38 35 33 bytes



                D,f,@:,d3*1+$2/iA2%D
                +?
                O
                Wx,$f>x


                Try it online!



                How it works



                First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,



                $$f(x) = begincases
                x : textis even, & 3x+1 \
                x : textis odd, & lfloorfracx2rfloor
                endcases$$



                When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.



                We then duplicate this value (d), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+), swap the top two values, then calculate $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.



                Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfracx2rfloor, (x : % : 2)]$. Finally, we use D to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfracx2rfloor$.



                That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x. However, x can be omitted from commands where it is obviously the other argument.



                For example, +? is identical to x+?, and assigns the input to x, but as x is the active variable, it can be omitted. Next, we output x, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x. All this does is run $f(x)$, then assign that to x, updating x on each iteration of the loop.






                share|improve this answer






















                • Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
                  – Eduardo Hoefel
                  39 mins ago










                • @EduardoHoefel Break line?
                  – caird coinheringaahing
                  38 mins ago










                • @cairdcoinheringaahing The newline characters, presumably.
                  – Lynn
                  4 mins ago

















                up vote
                0
                down vote














                Wolfram Language (Mathematica), 35 bytes



                0<Echo@#&&#0[3#+1-(5#+3)/2#~Mod~2]&


                Try it online!



                0<Echo@# && ...& is short-circuit evaluation: it prints the input #, checks if it's positive, and if so, evaluates .... In this case, ... is #0[3#+1-(5#+3)/2#~Mod~2]; since #0 (the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2, which simplifies to 3#+1 when # is even, and (#-1)/2 when # is odd.






                share|improve this answer



























                  up vote
                  0
                  down vote













                  Python 2, 52 bytes



                  n=input()
                  while n:print n;n=(n*3+1,n/2)[n%2]
                  print 0


                  -2 bytes thanks to Mr. Xcoder



                  There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.



                  Try it online!






                  share|improve this answer






















                  • -2 bytes
                    – Mr. Xcoder
                    29 mins ago










                  • @Mr.Xcoder Ah, thanks.
                    – Quintec
                    27 mins ago

















                  up vote
                  0
                  down vote














                  Common Lisp, 79 bytes





                  (defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))


                  Try it online!






                  share|improve this answer




















                    Your Answer





                    StackExchange.ifUsing("editor", function ()
                    return StackExchange.using("mathjaxEditing", function ()
                    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
                    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
                    );
                    );
                    , "mathjax-editing");

                    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: "200"
                    ;
                    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: false,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: null,
                    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%2fcodegolf.stackexchange.com%2fquestions%2f175248%2fthe-inverse-collatz-conjecture%23new-answer', 'question_page');

                    );

                    Post as a guest






























                    11 Answers
                    11






                    active

                    oldest

                    votes








                    11 Answers
                    11






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    0
                    down vote














                    Jelly, 9 bytes



                    :++‘ƊḂ?Ƭ2


                    Try it online!






                    share|improve this answer
























                      up vote
                      0
                      down vote














                      Jelly, 9 bytes



                      :++‘ƊḂ?Ƭ2


                      Try it online!






                      share|improve this answer






















                        up vote
                        0
                        down vote










                        up vote
                        0
                        down vote










                        Jelly, 9 bytes



                        :++‘ƊḂ?Ƭ2


                        Try it online!






                        share|improve this answer













                        Jelly, 9 bytes



                        :++‘ƊḂ?Ƭ2


                        Try it online!







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 1 hour ago









                        Erik the Outgolfer

                        30k42899




                        30k42899




















                            up vote
                            0
                            down vote













                            JavaScript (ES6), 31 bytes





                            f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)


                            Try it online!



                            Or 30 bytes in reverse order.






                            share|improve this answer


























                              up vote
                              0
                              down vote













                              JavaScript (ES6), 31 bytes





                              f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)


                              Try it online!



                              Or 30 bytes in reverse order.






                              share|improve this answer
























                                up vote
                                0
                                down vote










                                up vote
                                0
                                down vote









                                JavaScript (ES6), 31 bytes





                                f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)


                                Try it online!



                                Or 30 bytes in reverse order.






                                share|improve this answer














                                JavaScript (ES6), 31 bytes





                                f=n=>n&&n+' '+f(n&1?n>>1:n*3+1)


                                Try it online!



                                Or 30 bytes in reverse order.







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited 1 hour ago

























                                answered 1 hour ago









                                Arnauld

                                67.2k584283




                                67.2k584283




















                                    up vote
                                    0
                                    down vote














                                    Rust, 65 bytes





                                    fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);


                                    Try it online!






                                    share|improve this answer
























                                      up vote
                                      0
                                      down vote














                                      Rust, 65 bytes





                                      fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);


                                      Try it online!






                                      share|improve this answer






















                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote










                                        Rust, 65 bytes





                                        fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);


                                        Try it online!






                                        share|improve this answer













                                        Rust, 65 bytes





                                        fn f(n:u8)print!(" ",n);if n>0f(if n&1>0n>>1elsen*3+1);


                                        Try it online!







                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered 1 hour ago









                                        Herman L

                                        3,226428




                                        3,226428




















                                            up vote
                                            0
                                            down vote














                                            Pyth, 12 bytes



                                            .u?%N2/N2h*3


                                            Try it here as a test suite!






                                            share|improve this answer
























                                              up vote
                                              0
                                              down vote














                                              Pyth, 12 bytes



                                              .u?%N2/N2h*3


                                              Try it here as a test suite!






                                              share|improve this answer






















                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote










                                                Pyth, 12 bytes



                                                .u?%N2/N2h*3


                                                Try it here as a test suite!






                                                share|improve this answer













                                                Pyth, 12 bytes



                                                .u?%N2/N2h*3


                                                Try it here as a test suite!







                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered 1 hour ago









                                                Mr. Xcoder

                                                31.1k758197




                                                31.1k758197




















                                                    up vote
                                                    0
                                                    down vote














                                                    JAEL, 12 chars, 24 bytes



                                                    ![⍩î?ûḄ|õÀ⸵ẘ


                                                    I would post the TIO link but it's not working as expected there.



                                                    Right now, the input must be prepended to the code, like:



                                                    14![⍩î?ûḄ|õÀ⸵ẘ





                                                    share|improve this answer
























                                                      up vote
                                                      0
                                                      down vote














                                                      JAEL, 12 chars, 24 bytes



                                                      ![⍩î?ûḄ|õÀ⸵ẘ


                                                      I would post the TIO link but it's not working as expected there.



                                                      Right now, the input must be prepended to the code, like:



                                                      14![⍩î?ûḄ|õÀ⸵ẘ





                                                      share|improve this answer






















                                                        up vote
                                                        0
                                                        down vote










                                                        up vote
                                                        0
                                                        down vote










                                                        JAEL, 12 chars, 24 bytes



                                                        ![⍩î?ûḄ|õÀ⸵ẘ


                                                        I would post the TIO link but it's not working as expected there.



                                                        Right now, the input must be prepended to the code, like:



                                                        14![⍩î?ûḄ|õÀ⸵ẘ





                                                        share|improve this answer













                                                        JAEL, 12 chars, 24 bytes



                                                        ![⍩î?ûḄ|õÀ⸵ẘ


                                                        I would post the TIO link but it's not working as expected there.



                                                        Right now, the input must be prepended to the code, like:



                                                        14![⍩î?ûḄ|õÀ⸵ẘ






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered 1 hour ago









                                                        Eduardo Hoefel

                                                        415




                                                        415




















                                                            up vote
                                                            0
                                                            down vote













                                                            perl -Minteger -nlE, 39 bytes



                                                            say;$_=$_%2?$_/2:3*$_+1 and redosay 0





                                                            share|improve this answer
























                                                              up vote
                                                              0
                                                              down vote













                                                              perl -Minteger -nlE, 39 bytes



                                                              say;$_=$_%2?$_/2:3*$_+1 and redosay 0





                                                              share|improve this answer






















                                                                up vote
                                                                0
                                                                down vote










                                                                up vote
                                                                0
                                                                down vote









                                                                perl -Minteger -nlE, 39 bytes



                                                                say;$_=$_%2?$_/2:3*$_+1 and redosay 0





                                                                share|improve this answer












                                                                perl -Minteger -nlE, 39 bytes



                                                                say;$_=$_%2?$_/2:3*$_+1 and redosay 0






                                                                share|improve this answer












                                                                share|improve this answer



                                                                share|improve this answer










                                                                answered 1 hour ago









                                                                Abigail

                                                                37716




                                                                37716




















                                                                    up vote
                                                                    0
                                                                    down vote














                                                                    Clean, 53 bytes



                                                                    import StdEnv
                                                                    $0=[0]
                                                                    $n=[n: $if(isOdd n)(n/2)(n*3+1)]


                                                                    Try it online!






                                                                    share|improve this answer
























                                                                      up vote
                                                                      0
                                                                      down vote














                                                                      Clean, 53 bytes



                                                                      import StdEnv
                                                                      $0=[0]
                                                                      $n=[n: $if(isOdd n)(n/2)(n*3+1)]


                                                                      Try it online!






                                                                      share|improve this answer






















                                                                        up vote
                                                                        0
                                                                        down vote










                                                                        up vote
                                                                        0
                                                                        down vote










                                                                        Clean, 53 bytes



                                                                        import StdEnv
                                                                        $0=[0]
                                                                        $n=[n: $if(isOdd n)(n/2)(n*3+1)]


                                                                        Try it online!






                                                                        share|improve this answer













                                                                        Clean, 53 bytes



                                                                        import StdEnv
                                                                        $0=[0]
                                                                        $n=[n: $if(isOdd n)(n/2)(n*3+1)]


                                                                        Try it online!







                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered 58 mins ago









                                                                        Οurous

                                                                        5,67311031




                                                                        5,67311031




















                                                                            up vote
                                                                            0
                                                                            down vote














                                                                            Add++, 38 35 33 bytes



                                                                            D,f,@:,d3*1+$2/iA2%D
                                                                            +?
                                                                            O
                                                                            Wx,$f>x


                                                                            Try it online!



                                                                            How it works



                                                                            First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,



                                                                            $$f(x) = begincases
                                                                            x : textis even, & 3x+1 \
                                                                            x : textis odd, & lfloorfracx2rfloor
                                                                            endcases$$



                                                                            When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.



                                                                            We then duplicate this value (d), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+), swap the top two values, then calculate $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.



                                                                            Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfracx2rfloor, (x : % : 2)]$. Finally, we use D to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfracx2rfloor$.



                                                                            That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x. However, x can be omitted from commands where it is obviously the other argument.



                                                                            For example, +? is identical to x+?, and assigns the input to x, but as x is the active variable, it can be omitted. Next, we output x, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x. All this does is run $f(x)$, then assign that to x, updating x on each iteration of the loop.






                                                                            share|improve this answer






















                                                                            • Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
                                                                              – Eduardo Hoefel
                                                                              39 mins ago










                                                                            • @EduardoHoefel Break line?
                                                                              – caird coinheringaahing
                                                                              38 mins ago










                                                                            • @cairdcoinheringaahing The newline characters, presumably.
                                                                              – Lynn
                                                                              4 mins ago














                                                                            up vote
                                                                            0
                                                                            down vote














                                                                            Add++, 38 35 33 bytes



                                                                            D,f,@:,d3*1+$2/iA2%D
                                                                            +?
                                                                            O
                                                                            Wx,$f>x


                                                                            Try it online!



                                                                            How it works



                                                                            First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,



                                                                            $$f(x) = begincases
                                                                            x : textis even, & 3x+1 \
                                                                            x : textis odd, & lfloorfracx2rfloor
                                                                            endcases$$



                                                                            When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.



                                                                            We then duplicate this value (d), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+), swap the top two values, then calculate $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.



                                                                            Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfracx2rfloor, (x : % : 2)]$. Finally, we use D to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfracx2rfloor$.



                                                                            That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x. However, x can be omitted from commands where it is obviously the other argument.



                                                                            For example, +? is identical to x+?, and assigns the input to x, but as x is the active variable, it can be omitted. Next, we output x, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x. All this does is run $f(x)$, then assign that to x, updating x on each iteration of the loop.






                                                                            share|improve this answer






















                                                                            • Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
                                                                              – Eduardo Hoefel
                                                                              39 mins ago










                                                                            • @EduardoHoefel Break line?
                                                                              – caird coinheringaahing
                                                                              38 mins ago










                                                                            • @cairdcoinheringaahing The newline characters, presumably.
                                                                              – Lynn
                                                                              4 mins ago












                                                                            up vote
                                                                            0
                                                                            down vote










                                                                            up vote
                                                                            0
                                                                            down vote










                                                                            Add++, 38 35 33 bytes



                                                                            D,f,@:,d3*1+$2/iA2%D
                                                                            +?
                                                                            O
                                                                            Wx,$f>x


                                                                            Try it online!



                                                                            How it works



                                                                            First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,



                                                                            $$f(x) = begincases
                                                                            x : textis even, & 3x+1 \
                                                                            x : textis odd, & lfloorfracx2rfloor
                                                                            endcases$$



                                                                            When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.



                                                                            We then duplicate this value (d), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+), swap the top two values, then calculate $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.



                                                                            Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfracx2rfloor, (x : % : 2)]$. Finally, we use D to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfracx2rfloor$.



                                                                            That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x. However, x can be omitted from commands where it is obviously the other argument.



                                                                            For example, +? is identical to x+?, and assigns the input to x, but as x is the active variable, it can be omitted. Next, we output x, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x. All this does is run $f(x)$, then assign that to x, updating x on each iteration of the loop.






                                                                            share|improve this answer















                                                                            Add++, 38 35 33 bytes



                                                                            D,f,@:,d3*1+$2/iA2%D
                                                                            +?
                                                                            O
                                                                            Wx,$f>x


                                                                            Try it online!



                                                                            How it works



                                                                            First, we begin by defining a function $f(x)$, that takes a single argument, performs the inverted Collatz operation on $x$ then outputs the result. That is,



                                                                            $$f(x) = begincases
                                                                            x : textis even, & 3x+1 \
                                                                            x : textis odd, & lfloorfracx2rfloor
                                                                            endcases$$



                                                                            When in function mode, Add++ uses a stack memory model, otherwise variables are used. When calculating $f(x)$, the stack initially looks like $S = [x]$.



                                                                            We then duplicate this value (d), to yield $S = [x, x]$. We then yield the first possible option, $3x + 1$ (3*1+), swap the top two values, then calculate $lfloorfracx2rfloor$, leaving $S = [3x+1, lfloorfracx2rfloor]$.



                                                                            Next, we push $x$ to $S$, and calculate the bit of $x$ i.e. $x : % : 2$, where $a : % : b$ denotes the remainder when dividing $a$ by $b$. This leaves us with $S = [3x+1, lfloorfracx2rfloor, (x : % : 2)]$. Finally, we use D to select the element at the index specified by $(x : % : 2)$. If that's $0$, we return the first element i.e. $3x+1$, otherwise we return the second element, $lfloorfracx2rfloor$.



                                                                            That completes the definition of $f(x)$, however, we haven't yet put it into practice. The next three lines have switched from function mode into vanilla mode, where we operate on variables. To be more precise, in this program, we only operate on one variable, the active variable, represented by the letter x. However, x can be omitted from commands where it is obviously the other argument.



                                                                            For example, +? is identical to x+?, and assigns the input to x, but as x is the active variable, it can be omitted. Next, we output x, then entire the while loop, which loops for as long as $x neq 0$. The loop is very simple, consisting of a single statement: $f>x. All this does is run $f(x)$, then assign that to x, updating x on each iteration of the loop.







                                                                            share|improve this answer














                                                                            share|improve this answer



                                                                            share|improve this answer








                                                                            edited 58 mins ago

























                                                                            answered 1 hour ago









                                                                            caird coinheringaahing

                                                                            7,38032985




                                                                            7,38032985











                                                                            • Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
                                                                              – Eduardo Hoefel
                                                                              39 mins ago










                                                                            • @EduardoHoefel Break line?
                                                                              – caird coinheringaahing
                                                                              38 mins ago










                                                                            • @cairdcoinheringaahing The newline characters, presumably.
                                                                              – Lynn
                                                                              4 mins ago
















                                                                            • Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
                                                                              – Eduardo Hoefel
                                                                              39 mins ago










                                                                            • @EduardoHoefel Break line?
                                                                              – caird coinheringaahing
                                                                              38 mins ago










                                                                            • @cairdcoinheringaahing The newline characters, presumably.
                                                                              – Lynn
                                                                              4 mins ago















                                                                            Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
                                                                            – Eduardo Hoefel
                                                                            39 mins ago




                                                                            Just to understand: Is the break line part of the code? Or is it just for better explanation? I don't really know this language.
                                                                            – Eduardo Hoefel
                                                                            39 mins ago












                                                                            @EduardoHoefel Break line?
                                                                            – caird coinheringaahing
                                                                            38 mins ago




                                                                            @EduardoHoefel Break line?
                                                                            – caird coinheringaahing
                                                                            38 mins ago












                                                                            @cairdcoinheringaahing The newline characters, presumably.
                                                                            – Lynn
                                                                            4 mins ago




                                                                            @cairdcoinheringaahing The newline characters, presumably.
                                                                            – Lynn
                                                                            4 mins ago










                                                                            up vote
                                                                            0
                                                                            down vote














                                                                            Wolfram Language (Mathematica), 35 bytes



                                                                            0<Echo@#&&#0[3#+1-(5#+3)/2#~Mod~2]&


                                                                            Try it online!



                                                                            0<Echo@# && ...& is short-circuit evaluation: it prints the input #, checks if it's positive, and if so, evaluates .... In this case, ... is #0[3#+1-(5#+3)/2#~Mod~2]; since #0 (the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2, which simplifies to 3#+1 when # is even, and (#-1)/2 when # is odd.






                                                                            share|improve this answer
























                                                                              up vote
                                                                              0
                                                                              down vote














                                                                              Wolfram Language (Mathematica), 35 bytes



                                                                              0<Echo@#&&#0[3#+1-(5#+3)/2#~Mod~2]&


                                                                              Try it online!



                                                                              0<Echo@# && ...& is short-circuit evaluation: it prints the input #, checks if it's positive, and if so, evaluates .... In this case, ... is #0[3#+1-(5#+3)/2#~Mod~2]; since #0 (the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2, which simplifies to 3#+1 when # is even, and (#-1)/2 when # is odd.






                                                                              share|improve this answer






















                                                                                up vote
                                                                                0
                                                                                down vote










                                                                                up vote
                                                                                0
                                                                                down vote










                                                                                Wolfram Language (Mathematica), 35 bytes



                                                                                0<Echo@#&&#0[3#+1-(5#+3)/2#~Mod~2]&


                                                                                Try it online!



                                                                                0<Echo@# && ...& is short-circuit evaluation: it prints the input #, checks if it's positive, and if so, evaluates .... In this case, ... is #0[3#+1-(5#+3)/2#~Mod~2]; since #0 (the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2, which simplifies to 3#+1 when # is even, and (#-1)/2 when # is odd.






                                                                                share|improve this answer













                                                                                Wolfram Language (Mathematica), 35 bytes



                                                                                0<Echo@#&&#0[3#+1-(5#+3)/2#~Mod~2]&


                                                                                Try it online!



                                                                                0<Echo@# && ...& is short-circuit evaluation: it prints the input #, checks if it's positive, and if so, evaluates .... In this case, ... is #0[3#+1-(5#+3)/2#~Mod~2]; since #0 (the zeroth slot) is the function itself, this is a recursive call on 3#+1-(5#+3)/2#~Mod~2, which simplifies to 3#+1 when # is even, and (#-1)/2 when # is odd.







                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered 37 mins ago









                                                                                Misha Lavrov

                                                                                3,771323




                                                                                3,771323




















                                                                                    up vote
                                                                                    0
                                                                                    down vote













                                                                                    Python 2, 52 bytes



                                                                                    n=input()
                                                                                    while n:print n;n=(n*3+1,n/2)[n%2]
                                                                                    print 0


                                                                                    -2 bytes thanks to Mr. Xcoder



                                                                                    There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.



                                                                                    Try it online!






                                                                                    share|improve this answer






















                                                                                    • -2 bytes
                                                                                      – Mr. Xcoder
                                                                                      29 mins ago










                                                                                    • @Mr.Xcoder Ah, thanks.
                                                                                      – Quintec
                                                                                      27 mins ago














                                                                                    up vote
                                                                                    0
                                                                                    down vote













                                                                                    Python 2, 52 bytes



                                                                                    n=input()
                                                                                    while n:print n;n=(n*3+1,n/2)[n%2]
                                                                                    print 0


                                                                                    -2 bytes thanks to Mr. Xcoder



                                                                                    There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.



                                                                                    Try it online!






                                                                                    share|improve this answer






















                                                                                    • -2 bytes
                                                                                      – Mr. Xcoder
                                                                                      29 mins ago










                                                                                    • @Mr.Xcoder Ah, thanks.
                                                                                      – Quintec
                                                                                      27 mins ago












                                                                                    up vote
                                                                                    0
                                                                                    down vote










                                                                                    up vote
                                                                                    0
                                                                                    down vote









                                                                                    Python 2, 52 bytes



                                                                                    n=input()
                                                                                    while n:print n;n=(n*3+1,n/2)[n%2]
                                                                                    print 0


                                                                                    -2 bytes thanks to Mr. Xcoder



                                                                                    There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.



                                                                                    Try it online!






                                                                                    share|improve this answer














                                                                                    Python 2, 52 bytes



                                                                                    n=input()
                                                                                    while n:print n;n=(n*3+1,n/2)[n%2]
                                                                                    print 0


                                                                                    -2 bytes thanks to Mr. Xcoder



                                                                                    There must certainly be a faster way. Oddly, when I tried a lambda it was the same bytecount. I'm probably hallucinating.



                                                                                    Try it online!







                                                                                    share|improve this answer














                                                                                    share|improve this answer



                                                                                    share|improve this answer








                                                                                    edited 27 mins ago

























                                                                                    answered 48 mins ago









                                                                                    Quintec

                                                                                    1,065517




                                                                                    1,065517











                                                                                    • -2 bytes
                                                                                      – Mr. Xcoder
                                                                                      29 mins ago










                                                                                    • @Mr.Xcoder Ah, thanks.
                                                                                      – Quintec
                                                                                      27 mins ago
















                                                                                    • -2 bytes
                                                                                      – Mr. Xcoder
                                                                                      29 mins ago










                                                                                    • @Mr.Xcoder Ah, thanks.
                                                                                      – Quintec
                                                                                      27 mins ago















                                                                                    -2 bytes
                                                                                    – Mr. Xcoder
                                                                                    29 mins ago




                                                                                    -2 bytes
                                                                                    – Mr. Xcoder
                                                                                    29 mins ago












                                                                                    @Mr.Xcoder Ah, thanks.
                                                                                    – Quintec
                                                                                    27 mins ago




                                                                                    @Mr.Xcoder Ah, thanks.
                                                                                    – Quintec
                                                                                    27 mins ago










                                                                                    up vote
                                                                                    0
                                                                                    down vote














                                                                                    Common Lisp, 79 bytes





                                                                                    (defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))


                                                                                    Try it online!






                                                                                    share|improve this answer
























                                                                                      up vote
                                                                                      0
                                                                                      down vote














                                                                                      Common Lisp, 79 bytes





                                                                                      (defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))


                                                                                      Try it online!






                                                                                      share|improve this answer






















                                                                                        up vote
                                                                                        0
                                                                                        down vote










                                                                                        up vote
                                                                                        0
                                                                                        down vote










                                                                                        Common Lisp, 79 bytes





                                                                                        (defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))


                                                                                        Try it online!






                                                                                        share|improve this answer













                                                                                        Common Lisp, 79 bytes





                                                                                        (defun x(n)(cons n(if(= n 0)nil(if(=(mod n 2)0)(x(+(* n 3)1))(x(/(- n 1)2))))))


                                                                                        Try it online!







                                                                                        share|improve this answer












                                                                                        share|improve this answer



                                                                                        share|improve this answer










                                                                                        answered 17 mins ago









                                                                                        JRowan

                                                                                        2014




                                                                                        2014



























                                                                                             

                                                                                            draft saved


                                                                                            draft discarded















































                                                                                             


                                                                                            draft saved


                                                                                            draft discarded














                                                                                            StackExchange.ready(
                                                                                            function ()
                                                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175248%2fthe-inverse-collatz-conjecture%23new-answer', 'question_page');

                                                                                            );

                                                                                            Post as a guest













































































                                                                                            Comments

                                                                                            Popular posts from this blog

                                                                                            Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                                                                            Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                                                                                            Confectionery