16
\$\begingroup\$

Introduction

The sign of a number is either a +, or a - for every non-zero integer. Zero itself is signless (+0 is the same as -0). In the following sequence, we are going to alternate between the positive sign, the zero and the negative sign. The sequence starts with 1, so we write 1 with a positive sign, with zero (this one is weird, but we just multiply the number by 0) and the negative sign:

1, 0, -1

The next number is 2, and we do the same thing again:

2, 0, -2

The sequence eventually is:

1, 0, -1, 2, 0, -2, 3, 0, -3, 4, 0, -4, 5, 0, -5, 6, 0, -6, 7, 0, -7, ...

Or a more readable form:

a(0) = 1
a(1) = 0
a(2) = -1
a(3) = 2
a(4) = 0
a(5) = -2
a(6) = 3
a(7) = 0
a(8) = -3
a(9) = 4
...

The Task

Given a non-negative integer n, output the nth term of the above sequence. You can choose if you use the zero-indexed or one-indexed version.

Test cases:

Zero-indexed:

a(0) = 1
a(11) = -4
a(76) = 0
a(134) = -45
a(296) = -99

Or if you prefer one-indexed:

a(1) = 1
a(12) = -4
a(77) = 0
a(135) = -45
a(297) = -99

This is , so the submission with the smallest number of bytes wins!

\$\endgroup\$
  • \$\begingroup\$ Is it Ok if you start with [0, 0, 0, -1, 0, 1... \$\endgroup\$ – Blue May 28 '16 at 16:54
  • \$\begingroup\$ @muddyfish no sorry, it has to start with 1. \$\endgroup\$ – Adnan May 28 '16 at 16:59

47 Answers 47

1
\$\begingroup\$

GNU Coreutils, 27 Bytes

echo "(1+$1/3)*(1-$1%3)"|bc
\$\endgroup\$
  • \$\begingroup\$ I count 27 bytes. But this is even shorter: bc<<<"(1+$1/3)*(1-$1%3)" \$\endgroup\$ – Digital Trauma May 31 '16 at 5:24
  • \$\begingroup\$ @DigitalTrauma, I wrote exactly this answer before, but I am not sure if <<< is POSIX-ly correct, and I want to avoid Bash-ism here, as I have a plain Bash answer here, too. \$\endgroup\$ – rexkogitans May 31 '16 at 6:58
1
\$\begingroup\$

JavaScript, 22 bytes

r=n%3
o=(1-r)/3*(n-r+3)

n is the number you wish to call the function on. o is the output number.

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

APL, 14 bytes

{⍵⌷∊{⍵0,-⍵}¨⍳⍵}

This is 1-indexed, i.e.:

     {⍵⌷∊{⍵0,-⍵}¨⍳⍵}¨1 2 3 4 5 6 7 8
1 0 ¯1 2 0 ¯2 3 0

Explanation:

{⍵⌷                   select the ⍵th element
   ∊                  from all elements in
    {⍵0,-⍵}           the values ⍵, 0, and -⍵
           ¨⍳⍵}       for each number in 1..⍵
\$\endgroup\$
1
\$\begingroup\$

CJam, 11 bytes

ri3+3md(W**

0-based input.

Test it here.

Explanation

ri   e# Read input and convert to integer N.
3+   e# Add 3.
3md  e# Divmod 3, putting N/3+1 and N%3 on the stack.
(W*  e# Decrement, multiply by -1, turning 0, 1, 2 into 1, 0, -1, respectively.
*    e# Multiply.
\$\endgroup\$
1
\$\begingroup\$

><>, 16+3 = 19 bytes

:3%:1$-}-3,1+*n;

Needs the input to be present on the stack, so +3 bytes for the -v flag. Try it online!

The program outputs the zero-indexed sequence, using the formula:

f(n) = ((n-(n%3))/3) * (1-(n%3))

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

Java 7, 36 bytes

int c(int n){return(n/3+1)*(1-n%3);}

Ungolfed & test code:

Try it here.

class Main{

    static int c(int n){
        return (n / 3 + 1) * (1 - n % 3);
    }

    public static void main(String[] a){
        System.out.println(c(0));
        System.out.println(c(11));
        System.out.println(c(76));
        System.out.println(c(134));
        System.out.println(c(296));
    }
}

Zero-indexed output:

1
-4
0
-45
-99
\$\endgroup\$
1
\$\begingroup\$

Desmos, 125 86 bytes

b=\operatorname{ceil}\left(\frac{a}{3}\right)
b\operatorname{mod}\left(-a,3\right)-b
a=1

Simple fix for \operatorname{ceil}\left(\frac{a}{3}\right)

125 bytes:

\operatorname{ceil}\left(\frac{a}{3}\right)\operatorname{mod}\left(-a,3\right)-\operatorname{ceil}\left(\frac{a}{3}\right)
a=1

This is as close as I could get to divmod. Might be able to get rid of subtracting -\operatorname{ceil}\left(\frac{a}{3}\right) to shave some bytes off.

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

C#, 34 bytes

n=>n%3==1?0:(n/3+1)*(n%3==0?1:-1);

To assign the lambda to a variable, write:

Func<int,int>f=n=>n%3==1?0:(n/3+1)*(n%3==0?1:-1);

With C# 6, you can also assign the lambda to a method:

int F(int n)=>n%3==1?0:(n/3+1)*(n%3==0?1:-1);
\$\endgroup\$
1
\$\begingroup\$

Python 3, 30 25 bytes

I tried to replicate the results of Leaky Nun's Python post for 10 minutes before realising it was for Python 2, not 3.

Zero-indexed:

lambda n:(n//3+1)*(1-n%3)

Full program:

a = lambda n:(n//3+1)*(1-n%3)
print(a(0))   #   1
print(a(11))  #  -4
print(a(76))  #   0
print(a(134)) # -45
print(a(296)) # -99
\$\endgroup\$
  • \$\begingroup\$ Hello, and welcome to PPCG! \$\endgroup\$ – NoOneIsHere Jun 23 '16 at 23:34
  • \$\begingroup\$ Thanks for the welcome, @NoOneIsHere. Pleasure to be here! \$\endgroup\$ – act Jun 24 '16 at 19:36
1
\$\begingroup\$

Java, 19 bytes

Using lambda expressions.

i->(1+i/3)*(1-i%3);

Try it here!

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

Batch, 30 29 bytes

@cmd/cset/a(1+%1/3)*(1-%1%%3)

The cmd/c makes set/a echo the result of the calculation. Edit: Saved 1 byte by removing the unnecessary space.

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

Oasis, 12 bytes (non-competing)

n3÷>n3÷y>n-*

Try it online!

Oasis is a language designed by Adnan which is specialized in sequences.

Currently, this language can do recursion and closed form.

This answer can only demonstrate closed form. For recursion, see for example this answer.

We use this formula: a(n) = (n/3+1)*((n/3*3)+1-n) which is modified from the formula used in my Python answer, since there is no modulo at the moment.

n3÷>n3÷y>n-*

n             push n (input)
 3÷           integer-division by 3
   >          +1
    n         push n (input)
     3÷       integer-division by 3
       y      *3
        >     +1
         n    push n (input)
          -   subtract the top of stack from the second top of stack
           *  multiply the top of stack to the second top of stack
\$\endgroup\$
1
\$\begingroup\$

Cubix, 30 bytes

1..-.w>?^I3%?;,)O@...o-'.<u;;;

Try it here. You will need to replace the current code with the above and enter an input number.
This wraps onto a cube with an edge length of 3. I'm hoping to reduce this a bit more, but at the moment it's not to bad. It would be nice to have some of the planned features for the stack, but oh well.

      1 . .
      - . w
      > ? ^
I 3 % ? ; , ) O @ . . .
o - ' . < u ; ; ; . . .
. . . . . . . . . . . .
      . . .
      . . .
      . . .

Explanation:

  • I 3 % ? Take a number from input, push a literal 3, mod on TOS and do a check. The ? redirects right for negative and left for positive. Pass through for zero
  • For zero value ; , ) O @ Pop, integer divide TOS, increment TOS, output number and terminate
  • For positive value 1 - > ? Push literal 1, subtract TOS, change direction and check
    • For zero value ^ w O @ Redirect up, sidestep left (ends up on another face), output number (zero in this case) and terminate
    • For positive value ; < ' - o ; ; ; u , ) O @ pop, redirect, push character -, output character, pop, pop, pop, u-turn to left, integer divide TOS, increment TOS, output number and terminate
\$\endgroup\$
  • \$\begingroup\$ What happend to the other answer? :) \$\endgroup\$ – Adnan Jun 2 '16 at 23:58
  • \$\begingroup\$ My apologies for the edit mess up, I thought I had lost the previous post \$\endgroup\$ – MickyT Jun 2 '16 at 23:59
  • \$\begingroup\$ Oh, it's okay :) \$\endgroup\$ – Adnan Jun 3 '16 at 0:00
1
\$\begingroup\$

Python 2, 64 bytes

Although Leaky Nun has already posted a much shorter, brilliant Python answer, I decided to see whether this could be done recursively, calculating each new term from the last two. The result was interesting:

f=lambda n,a=1,b=0:(n<1)*`a`or f(n-1,b,[[0,-a][b==0],1-b][a==0])

Try it online!

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

Lua, 50 bytes

function a(n)print((math.floor(n/3)+1)*(1-n%3))end

Try it online!

\$\endgroup\$
0
\$\begingroup\$

VBA Excel , 46 bytes

using immediate window and Range A1 as input

a=[A1]:b=a Mod 3:?IIf(b=0,-a/3,IIf(b=1,a/3,0))
\$\endgroup\$
0
\$\begingroup\$

Common Lisp, 73 43 bytes

(lambda(n)(*(1+(floor n 3))(- 1(mod n 3))))

Try it online!

\$\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.