14
\$\begingroup\$

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!

\$\endgroup\$
  • 7
    \$\begingroup\$ Hmm ... this feels very familiar. \$\endgroup\$ – Shaggy Mar 26 '18 at 13:14
  • 3
    \$\begingroup\$ Related \$\endgroup\$ – Rod Mar 26 '18 at 13:22
  • \$\begingroup\$ @Rod Hooray! I got away with it! ;) \$\endgroup\$ – AJFaraday Mar 26 '18 at 13:23
  • \$\begingroup\$ Yep, that looks like the one I was thinking of, @Rod. \$\endgroup\$ – Shaggy Mar 26 '18 at 15:05
  • \$\begingroup\$ @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. \$\endgroup\$ – AJFaraday Mar 26 '18 at 21:03

30 Answers 30

5
\$\begingroup\$

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
\$\endgroup\$
  • 1
    \$\begingroup\$ You can leave out the b. \$\endgroup\$ – Adnan Mar 26 '18 at 17:07
  • \$\begingroup\$ I think you meant to multiply with 3, not add it. \$\endgroup\$ – Erik the Outgolfer Mar 26 '18 at 17:17
  • \$\begingroup\$ @EriktheOutgolfer The program computes a(n) as a(n-1)+3. \$\endgroup\$ – Dennis Mar 26 '18 at 17:18
12
\$\begingroup\$

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…!

\$\endgroup\$
11
\$\begingroup\$

Python 2, 20 bytes

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

Try it online!

\$\endgroup\$
10
\$\begingroup\$

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).

\$\endgroup\$
9
\$\begingroup\$

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

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

Try it online (Java 8).

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

Try it online (JavaScript).
Try it online (C# .NET).

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


Old 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

Try it online (JavaScript).
Try it online (C# .NET).

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
\$\endgroup\$
  • \$\begingroup\$ JavaScript polyglot, if you use a fat arrow. \$\endgroup\$ – Shaggy Mar 26 '18 at 15:19
  • \$\begingroup\$ @Shaggy Added, as well as C# .NET :) Although n=>(--n*3||4)-1 is also possible in JavaScript (also 14 bytes). \$\endgroup\$ – Kevin Cruijssen Mar 26 '18 at 16:06
7
\$\begingroup\$

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.

\$\endgroup\$
  • 3
    \$\begingroup\$ Congrats on 10k BTW! \$\endgroup\$ – Luis Mendo Mar 26 '18 at 13:23
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ – Giuseppe Mar 26 '18 at 13:47
  • 3
    \$\begingroup\$ @Giuseppe I know the feeling: 20k last week, as well as 2nd year anniversary. :) \$\endgroup\$ – Kevin Cruijssen Mar 26 '18 at 14:31
7
\$\begingroup\$

05AB1E, 8 7 bytes

3*s≠i4-

Try it online!

-1 byte thanks to Emigna !

\$\endgroup\$
  • \$\begingroup\$ @LuisMendo Thanks, fixed ! \$\endgroup\$ – Kaldo Mar 26 '18 at 13:35
  • 2
    \$\begingroup\$ 3*s≠i4- saves a byte \$\endgroup\$ – Emigna Mar 26 '18 at 13:59
  • \$\begingroup\$ @Emigna Thanks ! \$\endgroup\$ – Kaldo Mar 26 '18 at 14:12
6
\$\begingroup\$

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!

\$\endgroup\$
5
\$\begingroup\$

Haskell, 15 bytes

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

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Standard ML, 16 bytes

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

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Dodos, 27 bytes

	dot D
D
	
	d d
	d d
d
	dip

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Jelly, 6 bytes

++_>¡4

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Prolog (SWI), 21 bytes

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

Try it online!

\$\endgroup\$
4
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ 6 bytes porting Dennis' Jelly answer 2-|EG+ \$\endgroup\$ – Giuseppe Mar 27 '18 at 19:26
4
\$\begingroup\$

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|.
   +  Add; yield 2|n-2|+n.
\$\endgroup\$
3
\$\begingroup\$

APL (Dyalog), 9 bytes

3∘×-4×1∘<

Try it online!

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 20 bytes

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

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Alternative with the same byte-count: f(n){n=n<2?3:n*3-4;} \$\endgroup\$ – Kevin Cruijssen Mar 26 '18 at 17:38
  • \$\begingroup\$ Another alternative with the same byte count: f(n){n=n*3-4*(n>1);} \$\endgroup\$ – MD XF Mar 26 '18 at 21:39
3
\$\begingroup\$

MachineCode on x86_64, 34 32 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)
\$\endgroup\$
  • \$\begingroup\$ so what exactly is this language? \$\endgroup\$ – qwr Mar 28 '18 at 5:52
  • \$\begingroup\$ @qwr Check out the README in the repository for a simple description. \$\endgroup\$ – MD XF Mar 28 '18 at 20:39
3
\$\begingroup\$

><>, 10 9 bytes

Saved 1 byte thanks to Jo King

3*:3)4*-n

Try it online!

\$\endgroup\$
  • \$\begingroup\$ 9 bytes \$\endgroup\$ – Jo King Mar 27 '18 at 0:48
  • \$\begingroup\$ @JoKing: Don't kow how I missed that. Thanks! \$\endgroup\$ – Emigna Mar 27 '18 at 6:30
2
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ Why was this downvoted? \$\endgroup\$ – Uriel Mar 26 '18 at 14:17
  • \$\begingroup\$ 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. \$\endgroup\$ – AJFaraday Mar 26 '18 at 14:22
  • \$\begingroup\$ @AJFaraday it uses byte input and output, which is valid by meta consensus. see the explanation inside the input section \$\endgroup\$ – Uriel Mar 26 '18 at 14:26
  • \$\begingroup\$ Any resources on how to interpret the result? Or the input? \$\endgroup\$ – AJFaraday Mar 26 '18 at 14:27
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Uriel Mar 26 '18 at 14:30
2
\$\begingroup\$

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

\$\endgroup\$
2
\$\begingroup\$

TI-Basic, 8 bytes

3Ans-4(Ans>1
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 4 bytes

Uses the abs-method from Dennis' Jelly answer

2α·+

Try it online!

Explanation

2α      # abs(input, 2)
  ·     # multiply by 2
   +    # add input
\$\endgroup\$
2
\$\begingroup\$

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:

testing

\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
  • \$\begingroup\$ please describe which shell this is \$\endgroup\$ – qwr Mar 27 '18 at 22:27
  • \$\begingroup\$ tested on Cygwin ( CYGWIN_NT-10.0 2.3.1(0.291/5/3) 2015-11-14 12:44 x86_64 Cygwin) \$\endgroup\$ – Ali ISSA Mar 28 '18 at 5:36
  • \$\begingroup\$ can you write it in bc directly? \$\endgroup\$ – qwr Mar 28 '18 at 5:51
  • \$\begingroup\$ 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. \$\endgroup\$ – therealfarfetchd Mar 28 '18 at 18:29
  • 1
    \$\begingroup\$ F(){bc<<<$1*3-$(($1>1))*4} works in zsh though and removes 2 bytes \$\endgroup\$ – therealfarfetchd Mar 28 '18 at 18:35
1
\$\begingroup\$

Python 3, 48 bytes

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

Try It Online!

\$\endgroup\$
  • \$\begingroup\$ 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... \$\endgroup\$ – AJFaraday Mar 26 '18 at 21:06
  • \$\begingroup\$ @AJFaraday The footer of my post or of my code? \$\endgroup\$ – Nathan Dimmer Mar 26 '18 at 21:12
  • \$\begingroup\$ 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. \$\endgroup\$ – AJFaraday Mar 26 '18 at 21:16
  • \$\begingroup\$ 25 bytes \$\endgroup\$ – Jo King Mar 27 '18 at 0:16
  • \$\begingroup\$ @JoKing Do you know of a good guide to lambda functions in Python? I really don't understand how the syntax works. \$\endgroup\$ – Nathan Dimmer Mar 27 '18 at 0:32
1
\$\begingroup\$

J, 9 bytes

3&*-4*1&<

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
\$\begingroup\$

Ruby, 14 bytes

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

Try it online!

Uses the 1%1==0 trick as seen in other answers here.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.