# Two steps forward and one step back

Let's say I'm ten steps away from my destination. I walk there following the old saying, "Two steps forward and one step back". I take two steps forward, one back, until I'm standing exactly on my destination. (This might involve stepping past my destination, and returning to it). How many steps did I walk?

Of course, I might not be 10 steps away. I might be 11 steps away, or 100. I could measure ten paces, and keep walking back and forth to solve the problem, or... I could write some code!

• Write a function to work out how many steps it takes to get N steps away, in the sequence: two steps forward, one step back.
• Assume you've started at step 0. Count the "two steps forward" as two steps, not one.
• Assume all steps are a uniform length.
• It should return the number of steps first taken when you reach that space. (For instance, 10 steps away takes 26 steps, but you'd hit it again at step 30). We're interested in the 26.
• Use any language you like.
• It should accept any positive integer as input. This represents the target step.
• Smallest number of bytes win.

Example:

I want to get 5 steps away:

| | | | | | <- I'm at step 0, not yet on the grid.
| |X| | | | <- I take two steps forward, I'm on step 2: the count is 2
|X| | | | | <- I take one step back, I'm on step 1: the count is 3
| | |X| | | <- I take two steps forward, I'm on step 3: the count is 5
| |X| | | | <- I take one step back, I'm on step 2 again: the count is 6
| | | |X| | <- I take two steps forward, I'm on step 4: the count is 8
| | |X| | | <- I take one step back, I'm on step 3 again: the count is 9
| | | | |X| <- I take two steps forward, I'm on step 5: the count is 11


In this case, the result of the function would be 11.

Example results:

1      =>  3
5      =>  11
9      =>  23
10     =>  26
11     =>  29
100    =>  296
1000   =>  2996
10000  =>  29996
100000 =>  299996


Have fun, golfers!

• Hmm ... this feels very familiar. Commented Mar 26, 2018 at 13:14
• Related
– Rod
Commented Mar 26, 2018 at 13:22
• @Rod Hooray! I got away with it! ;) Commented Mar 26, 2018 at 13:23
• Yep, that looks like the one I was thinking of, @Rod. Commented Mar 26, 2018 at 15:05
• @Shaggy Rod changed his comment a little. The earlier one noted that the snails/wells question is asking for the number of iterations, but this is asking for the distance covered. Commented Mar 26, 2018 at 21:03

# Python 2, 18 bytes

lambda n:3*n-1%n*4


Try it online.

I picked this trick up from xnor just a few days ago…!

# Python 2, 20 bytes

lambda n:n*3-4*(n>1)


Try it online!

# Python 2, 17 bytes

lambda n:n-3%~n*2


Try it online!

I found the expression by brute-force search. It effectively computes n+2*abs(n-2).

# Polyglot: Java 8 / JavaScript / C# .NET, 1614 12 bytes

n->3*n-1%n*4


Try it online (Java 8).

n=>3*n-1%n*4


Port of @Lynn's Python 2 answer, so make sure to upvote his/her answer.

# Polyglot: Java 8 / JavaScript / C# .NET, 16 14 bytes

n->n<2?3:n*3-4


Try it online (Java 8).

n=>n<2?3:n*3-4


Explanation:

n->       // Method with integer as both parameter and return-type
n<2?    //  If the input is 1:
3      //   Return 3
:       //  Else:
n*3-4  //   Return the input multiplied by 3, and subtract 4

• JavaScript polyglot, if you use a fat arrow. Commented Mar 26, 2018 at 15:19
• @Shaggy Added, as well as C# .NET :) Although n=>(--n*3||4)-1 is also possible in JavaScript (also 14 bytes). Commented Mar 26, 2018 at 16:06

# R, 20 bytes

N=scan();3*N-4*(N>1)


Try it online!

Didn't notice the pattern until after I had implemented my less elegant solution.

• Congrats on 10k BTW! Commented Mar 26, 2018 at 13:23
• @LuisMendo thanks! I think my one-year anniversary on the site was a couple days ago, so it's been a good week for me, PPCG-wise. Commented Mar 26, 2018 at 13:47
• @Giuseppe I know the feeling: 20k last week, as well as 2nd year anniversary. :) Commented Mar 26, 2018 at 14:31

# 05AB1E, 8 7 bytes

3*s≠i4-


Try it online!

-1 byte thanks to Emigna !

• 3*s≠i4- saves a byte Commented Mar 26, 2018 at 13:59

# Oasis, 5 bytes

¹y4-3


Explanation:

    3  defines f(1) = 3
¹y4-   defines f(n) as:
¹      push input
y     triple
4-   subtract four


Try it online!

# Oasis, 5 4 bytes

1 byte saved thanks to @Adnan

3+23


Not to be confused with 23+3

Try it online!

How?

      implicitly push a(n-1)
3     push 3
+    sum and implicitly print
2   a(2) = 2
3  a(1) = 3

• You can leave out the b. Commented Mar 26, 2018 at 17:07
• I think you meant to multiply with 3, not add it. Commented Mar 26, 2018 at 17:17
• @EriktheOutgolfer The program computes a(n) as a(n-1)+3. Commented Mar 26, 2018 at 17:18

f 1=3
f n=3*n-4


Try it online!

# Standard ML, 16 bytes

fn 1=>3|n=>3*n-4


Try it online!

# Dodos, 27 bytes

	dot D
D

d d
d d
d
dip


Try it online!

# Jelly, 6 bytes

++_>¡4


Try it online!

# APL (Dyalog), 9 bytes

3∘×-4×1∘<


Try it online!

# Prolog (SWI), 21 bytes

1*3.
X*Y:-Y is 3*X-4.


Try it online!

# MATL, 7 bytes

Uses the 3*n-4*(n>1) formula. Multiply input by 3 (3*), push input again (G) and decrement it (q). If the result is not zero (?) then subtract 4 from the result (4-).

3*Gq?4-


Try it online!

• 6 bytes porting Dennis' Jelly answer 2-|EG+ Commented Mar 27, 2018 at 19:26

# Jelly, 4 bytes

ạ2Ḥ+


Try it online!

### How it works

ạ2Ḥ+  Main link. Argument: n

ạ2    Absolute difference with 2; yield |n-2|.
Ḥ   Unhalve/double; yield 2|n-2|.


# C (gcc), 20 bytes

f(n){n=3*n-4*!!~-n;}


Try it online!

• Alternative with the same byte-count: f(n){n=n<2?3:n*3-4;} Commented Mar 26, 2018 at 17:38
• Another alternative with the same byte count: f(n){n=n*3-4*(n>1);} Commented Mar 26, 2018 at 21:39

# MachineCode on x86_64, 3432 24 bytes

8d47fe9931d029d08d0447c3


Requires the i flag for integer output; input is taken via manually appending to the code.

Try it online!

I went through these 4 different C functions to find the 24-byte MachineCode program:

• n+2*abs(n-2) = 8d47fe9931d029d08d0447c3 (24 bytes)
• 3*n-4*!!~-n = 8d047f31d2ffcf0f95c2c1e20229d0c3 (32 bytes)
• n*3-4*(n>1) = 31d283ff028d047f0f9dc2c1e20229d0c3 (34 bytes)
• n<2?3:n*3-4 = 83ff01b8030000007e068d047f83e804c3 (34 bytes)
• so what exactly is this language?
– qwr
Commented Mar 28, 2018 at 5:52
• @qwr Check out the README in the repository for a simple description. Commented Mar 28, 2018 at 20:39

# ><>, 10 9 bytes

Saved 1 byte thanks to Jo King

3*:3)4*-n


Try it online!

• 9 bytes
– Jo King
Commented Mar 27, 2018 at 0:48
• @JoKing: Don't kow how I missed that. Thanks! Commented Mar 27, 2018 at 6:30

# 4, 54 bytes

3.6010160303604047002020003100000180010202046000095024


Try it online!

If you question the input method, please visit first the numerical input and output may be given as a character code meta post.

• Because the answer appears to be one quarter, which isn't a valid result. As far as I can tell, it doesn't solve the problem. Commented Mar 26, 2018 at 14:22
• @AJFaraday it uses byte input and output, which is valid by meta consensus. see the explanation inside the input section Commented Mar 26, 2018 at 14:26
• Any resources on how to interpret the result? Or the input? Commented Mar 26, 2018 at 14:27
• @AJFaraday the char code of the result is the answer. I've edited the question to include the relevant meta post. 4 has only char input. Commented Mar 26, 2018 at 14:30

# Japt, 7 bytes

A port of Lynn's Python solution.

*3É%U*4


Try it

## Alternative

This was a fun alternative to the closed formula solutions that is, unfortunately, a byte longer:

_+3}gN³²


Try it

# TI-Basic, 8 bytes

3Ans-4(Ans>1


# 05AB1E, 4 bytes

Uses the abs-method from Dennis' Jelly answer

2α·+


Try it online!

Explanation

2α      # abs(input, 2)
·     # multiply by 2


# 65816 machine code, 22 bytes

I could have made this 65C02 machine code easily for 3 bytes less, but didn't, since the register size on the 65C02 is 8-bit instead of 16-bit. It would work, but it's boring because you can only use really low numbers ;-)

xxd dump:

00000000: 7aa9 0000 aa89 0100 d004 8888 e824 c8e8  z............$.. 00000010: 1ac0 0000 d0ef ......  disassembly / code explanation: ; target is on the stack ply 7A ; pull target from stack lda #$0000       A9 00 00            ; set loop counter to 0
tax              AA                  ; set step counter to 0
loop:
bit #$0001 89 01 00 ; sets Z if loop counter is even bne odd D0 04 ; if Z is not set, jump to 'odd' dey 88 ; decrement target twice dey 88 inx E8 ; increment step counter .byte$24        24                  ; BIT $xx opcode, effectively skips the next byte odd: iny C8 ; increment target inx E8 ; increment step counter inc a 1A ; increment loop counter cpy #$0000       C0 00 00            ; sets zero flag, can be optimized maybe?
bne loop         D0 EF               ; if Y is non-zero, loop

; result is in register X


Testing it out on a 65816-compatible emulator:

SHELL , 28 Bytes

F(){ bc<<<$1*3-$(($1>1))*4;}  Tests : F 1 3 F 2 2 F 3 5 F 4 8 F5 11 F 11 29 F 100 296 F 100000 299996  Explanation : The formula is : if n == 1 ==> F(1) = 3 else F(n) = 3*n - 4  following the sequence of 3 steps "Two steps forward and one step back", we will have the arithmetic series :  +2 2 => 2 ( or 6 ) -1 1 => 3 ----------- +2 3 => 5 ( or 9 ) -1 2 => 6 ----------- +2 4 => 8 ( or 12 ) -1 3 => 9 ----------- +2 5 => 11 ( or 15 ) -1 4 => 12 ----------- +2 6 => 14 ( or 18 ) -1 5 => 15 ----------- +2 7 => 17 ( or 21 ) -1 6 => 18  At the minimum, or first coincidence :  1 => 3 2 => 2 3 => 5 4 => 8 5 => 11 6 => 14  in one formula : F(n) = 3*n - 4(n>1) with n>1 is 1 or 0 (if n==1)  • please describe which shell this is – qwr Commented Mar 27, 2018 at 22:27 • tested on Cygwin ( CYGWIN_NT-10.0 2.3.1(0.291/5/3) 2015-11-14 12:44 x86_64 Cygwin) Commented Mar 28, 2018 at 5:36 • can you write it in bc directly? – qwr Commented Mar 28, 2018 at 5:51 • I'm not familiar with bc, but since the argument of the function ($1) is used several times and some shell-specific stuff (arithmetic expansion, $((…))) is done, probably not. Commented Mar 28, 2018 at 18:29 • F(){bc<<<$1*3-$(($1>1))*4} works in zsh though and removes 2 bytes Commented Mar 28, 2018 at 18:35

# Python 3, 48 bytes

def a(x):
if x!=1:
return((3*x)-4)
return(3)


Try It Online!

• Nice work. You might want to put some code in the “Footer” section, too. That way you can test your function without padding out your golf entry... Commented Mar 26, 2018 at 21:06
• @AJFaraday The footer of my post or of my code? Commented Mar 26, 2018 at 21:12
• On Try It Online; you can add a footer which runs with your code but doesn’t count towards the byte length. Then the output will show your code at work. Commented Mar 26, 2018 at 21:16
• 25 bytes
– Jo King
Commented Mar 27, 2018 at 0:16
• @JoKing Do you know of a good guide to lambda functions in Python? I really don't understand how the syntax works. Commented Mar 27, 2018 at 0:32

# J, 9 bytes

3&*-4*1&<


Try it online!

# MATLAB/Octave, 15 bytes

@(n)3*n-4*(n>1)


Try it online!

Kind of surprised there isn't already a MATLAB answer. Same algorithm of 3*n-4 if greater than 1, or 3*n otherwise.

# Brain-Flak, 38 bytes

({<([()()]{})>()(){(<((){})>)()}{}}{})


Try it online!

The first answer I see to calculate the answer by stepping back and forth.

({ while not at 0
<([()()]{})>()() take two steps forward, counting 2 steps
{(<((){})>)()}{} take one step back, if not at 0, and add 1 step
}{}) remove the 0 and push step sum


## Wd, 7 bytes

♦óÖ╣░Θ\$


## Explanation

3*1a<4*-


Evaluates (a*3)-4*(a>1).

## Another possible alternative

3*1am4*-


Evaluates (a*3)-4*(1%a).