Skip to main content
1 of 3
MercyBeaucou
  • 3.1k
  • 13
  • 30

Befunge-98, 50 bytes

Normal

\+]#:\-1vk  !:;#
@k!:-1 .: ;#]#$ <;
[*:>@ 
&$< ^&;

This is by far the simplest program of the 4 - the only commands that are executed are the following:

\+]
  !
  : @
&$< ^&;

This program does some irrevelant stuff before hitting a "turn right" command (]) and an arrow. Then it hits 2 "take input" commands. Because there is only 1 number in input and because of how TIO treats &s, the program exits after 60 seconds. If there are 2 inputs (and because I can without adding bytes), the IP would travel up into the "end program" function.

Try it online!

Reversed

;&^ <$&
 @>:*[
;< $#]#; :. 1-:!k@
#;:!  kv1-\:#]+\

This one is a little more complicated. the relevant commands are as follows:

;&^  $
  >:*[
;< $#]#; :. 1-:!k@
  :

which is equivalent to

;&^                   Takes input and sends the IP up. the ; is a no-op
  :                   Duplicates the input.
  >:*[                Duplicates and multiplies, so that the stack is [N, N^2
     $                Drops the top of the stack, so that the top is N
     ]#;              Turns right, into the loop
         :.           Prints, because we have space and it's nice to do
            1-        Subtracts 1 from N
              :!k@    If (N=0), end the program. This is repeated until N=0
;< $#]#;              This bit is skipped on a loop because of the ;s, which comment out things

The important part here is the :. 1-:!k@ bit. It's useful because as long as we push the correct complexity onto the stack before executing in a lower time complexity, we can get the desired one. This will be used in the remaining 2 programs in this way.

Try it online!

Doubled

\+]#:\-1vk  !:;#
@k!:-1 .: ;#]#$ <;
[*:>@ 
&$< ^&;\+]#:\-1vk  !:;#
@k!:-1 .: ;#]#$ <;
[*:>@ 
&$< ^&;

And the relevant command are:

\+]
  !
  :
&$< ^&;\+]#:\-1vk  !:;#
@k!:-1 .: ;#]#$ <;

This program goes into 2 loops. First, it follows the same path as the normal program, which pushes 1 and N onto the stack, but instead of wrapping around to the second &, the IP jumps over a comment into a loop that pushes 2^N:

        vk!:    If N is 0, go to the next loop.
      -1        Subtract 1 from N
 +  :\          Pulls the 1 up to the top of the stack and doubles it
  ]#            A no-op
\               Pulls N-1 to the top again

The other bits on line 4 are skipped using ;s

After (2^N) is pushed onto the stack, we move down a line into the aforementioned printing loop. Because of the first loop, the time complexity is Θ(N + 2^N), but that reduces to Θ(2^N).

Try it online!

Doubled and Reversed

;&^ <$&
 @>:*[
;< $#]#; :. 1-:!k@
#;:!  kv1-\:#]+\;&^ <$&
 @>:*[
;< $#]#; :. 1-:!k@
#;:!  kv1-\:#]+\

The relevant commands:

;&^



 @>:*[
;< $#]#; :. 1-:!k@
  :

This functions almost identically to the reversed program, but the N^2 is not popped off of the stack, because the first line of the second copy of the program is appended to the last line of the first, meaning that the drop command ($) never gets executed, so we go into the printing loop with N^2 on the top of the stack.

Try it online!

MercyBeaucou
  • 3.1k
  • 13
  • 30