Dizzy integer enumeration

25

2

Your challenge today is to output a given term of a sequence enumerating all of the integers. The sequence is as follows: If we have a 0-indexed function generating the sequence f(n) and ceil(x) is the ceiling function, then f(0) = 0; abs(f(n)) = ceil(n/2); sign(f(n)) is positive when n and ceil(n/2) are either both even or both odd.

To help understand this sequence, the first few terms are as follows: 0 1 -1 -2 2 3 -3 -4 4 5 -5 -6 6 7 -7...

Your task is to write a program to that takes an integer n and outputs the nth term of the sequence. Input may be 0 or 1-indexed only.

Test cases (0-indexed):

0  =>  0
1  =>  1
2  => -1
3  => -2
4  =>  2
5  =>  3

This is , fewest bytes wins!

Pavel

Posted 2017-09-15T15:20:49.087

Reputation: 8 585

Related: Print all integers

– sergiol – 2017-09-15T22:18:52.980

It seems the inverse of a Folding function

– sergiol – 2017-09-17T00:59:17.540

Answers

8

SOGL V0.12, 8 6 bytes

I».»⌡±

Try it Here! or try the first couple numbers (changed a bit so it'd work)
0-indexed.

Explanation:

I       increment the input
 »      floor divide by 2
  .     push the original input
   »    floor divide by 2
    ⌡   that many times
     ±    negate

Or simpler:

(input + 1) // 2 negated input // 2 times
        I     »     ±      .     »    ⌡

dzaima

Posted 2017-09-15T15:20:49.087

Reputation: 19 048

3IT DIDN'T TAKE A SINGLE MINUTE! – NieDzejkob – 2017-09-15T15:22:13.970

6I ».» am on the phone I».»⌡±. – Jonathan Allan – 2017-09-15T16:08:24.377

@JonathanAllan I don't get it ._. – Pavel – 2017-09-17T22:16:47.123

9

Python 2, 26 24 bytes

lambda x:-~x/2/(1-(x&2))

Try it online!

Halvard Hummel

Posted 2017-09-15T15:20:49.087

Reputation: 3 131

1You could save a byte using -~-(x&2) for the final denominator. – recursive – 2017-09-17T01:45:06.373

5

JavaScript (ES6), 18 bytes

1-indexed.

n=>n/(++n&2||-2)|0

Demo

let f =

n=>n/(++n&2||-2)|0

for(n = 1; n < 20; n++) {
  console.log(n + ' --> ' + f(n))
}

Arnauld

Posted 2017-09-15T15:20:49.087

Reputation: 111 334

4

C, 25 bytes

f(n){return~n/2*~-(n&2);}

orlp

Posted 2017-09-15T15:20:49.087

Reputation: 37 067

You can save 4 bytes by assigning your return value to the first paramter instead of using the keyword return. f(n){n=~n/2*~-(n&2);} – cleblanc – 2017-09-15T20:11:44.310

5@cleblanc That's not how C works. – orlp – 2017-09-15T20:59:30.750

2gcc -O0 for x86-64 does happen to compile @cleblanc's version to instructions that happen to leave the multiply result in eax (https://godbolt.org/g/dztKPV), but then it would be an x86-64 gcc -O0 answer, not a C answer. I don't up-vote C answers that break with optimization enabled, especially not that stupid last expression as return-value crap. Even if that's how gcc happens to work, that's not how C works. – Peter Cordes – 2017-09-16T07:48:42.463

Make n a pointer. You don't need optimizations if the original and final values aren't on the stack. – mreff555 – 2017-09-16T15:37:05.847

1@mreff555 That would be a non-standard (although acceptable) IO method, and wouldn't be any shorter. – orlp – 2017-09-16T16:22:59.377

4

Haskell, 26 bytes

f x=div(x+1)2*(-1)^div x 2

Try it online!

The other Haskell answers seem to be overcomplicating things… ^^

Lynn

Posted 2017-09-15T15:20:49.087

Reputation: 55 648

3

Jelly, 6 bytes

HµĊN⁸¡

Try it online!

Uses dzaima's algorithm.

-1 thanks to Jonathan Allan.

Erik the Outgolfer

Posted 2017-09-15T15:20:49.087

Reputation: 38 134

SOGL can't be beating Jelly...? Will look into golfing. – Erik the Outgolfer – 2017-09-15T15:35:39.783

@JonathanAllan duh >_< – Erik the Outgolfer – 2017-09-15T16:04:23.327

3

Pyke, 6 bytes

heQeV_

Try it here!

Uses dzaima's approach... Beats Ties Jelly!

Explanation

h      - Increment the input, which is implicit at the beginning.
 e     - Floor halve.
  Q    - Push the input.
   e   - Floor halve.
    V_ - Apply repeatedly (V), ^ times, using negation (_).
       - Output implicitly.

The hex-encoded bytes equivalent would be: 68 65 51 65 56 5F.

Mr. Xcoder

Posted 2017-09-15T15:20:49.087

Reputation: 39 774

3

Haskell, 25 43 42 bytes

((do a<-[0..];[[-a,a],[a,-a]]!!mod a 2)!!)

Try it online! 1-indexed.

Edit: The previous version had the signs in a wrong order, thanks to @Potato44 for pointing out. Fixed for 18 bytes ...

Edit 2: Thanks to BMO for -1 byte!

Laikoni

Posted 2017-09-15T15:20:49.087

Reputation: 23 676

You can save 1 byte by using do-notation, try it online!

– ბიმო – 2018-01-08T15:04:16.533

@BMO Thanks! ... – Laikoni – 2018-01-08T20:02:58.807

3

Mathematica, 24 bytes

(s=⌈#/2⌉)(-1)^(#+s)&  

-14 bytes from @Misha Lavrov

J42161217

Posted 2017-09-15T15:20:49.087

Reputation: 15 931

1Using Boole and OddQ has the effect of converting odd numbers to 1 and even numbers to 0, but you don't need that here: powers of -1 give you the right answer for all odd numbers anyway. So you can cut down that step to (-1)^Tr@{#,s} or just (-1)^(#+s). – Misha Lavrov – 2017-09-24T01:02:40.867

3

Python 2, 21 bytes

lambda x:-x/2*~-(x&2)

Try it online!

Lynn

Posted 2017-09-15T15:20:49.087

Reputation: 55 648

2

Python 3, 29 bytes

lambda n:-~n//2*(-1)**(n%4>1)

Try it online!

Leaky Nun

Posted 2017-09-15T15:20:49.087

Reputation: 45 011

3(-1)**(n%4>1) is a rather convoluted way of writing (1-(n&2)) ;) – orlp – 2017-09-15T17:45:12.513

2

Pyth, 9 bytes

_F/hQ2/Q2

Try it here!

Uses dzaima's approach.

Mr. Xcoder

Posted 2017-09-15T15:20:49.087

Reputation: 39 774

2

Batch, 29 bytes

@cmd/cset/a"%1/2^(%1<<30>>30)

Neil

Posted 2017-09-15T15:20:49.087

Reputation: 95 035

2

Haskell, 36 bytes

g x=x: -x:g(-x-signum x)
((0:g 1)!!)

Try it online!

nimi

Posted 2017-09-15T15:20:49.087

Reputation: 34 639

2

05AB1E, 6 bytes

;DîsF(

Try it online!

Uses dzaima's algorithm.

Erik the Outgolfer

Posted 2017-09-15T15:20:49.087

Reputation: 38 134

2

JavaScript (ES6), 18 bytes

f=
n=>n/2^(n<<30>>30)
<input type=number min=0 value=0 oninput=o.textContent=f(this.value)><pre id=o>0

0-indexed.

Neil

Posted 2017-09-15T15:20:49.087

Reputation: 95 035

2

Javascript, 17 bytes

n=>~n/2*~-(n&2)^0

f=
n=>~n/2*~-(n&2)^0
<input type=number min=0 value=0 oninput=o.textContent=f(this.value)><pre id=o>0

This one is 0 indexed. It's entirely bitwise trickery.

recursive

Posted 2017-09-15T15:20:49.087

Reputation: 8 616

2

dc, 16 bytes

1+d2~+2%2*1-r2/*

I am sure there's a way to make 0..1 to -1..1 in dc shorter, but no ideas for now.

Try it online!

cab404

Posted 2017-09-15T15:20:49.087

Reputation: 141

2

Cubically, 23 bytes

(1-indexed)

FDF'$:7+8/0_0*0-8*7/0%6

Try it online!

The main difficulty when writing code in Cubically are:

  • There is only 1 write-able variable, and
  • Get constants is hard.

So, this solution calculate

((((n+1)/2)%2)*2-1)*n/2

where / denotes integer division. That only need 1 temporary variable, and constants 1 and 2.

Explanation:

FDF'$:7+8/0_0*0-8*7/0%6
FDF'                      Set face value of face 0 to 2, and value of memory index 8 (cube is unsolved) to 1 (true = unsolved)
    $                     Read input
     :7                                 input
       +8                                + 1
         /0                        (        ) /2
           _0                     (             ) %2
             *0                  (                  ) *2
               -8                                        -1
                 *7             (                          ) *n
                   /0                                          /2
                     %6   Print

user202729

Posted 2017-09-15T15:20:49.087

Reputation: 14 620

2

TI-Basic (TI-84 Plus CE), 20 bytes

‾int(‾Ans/2)(1-2remainder(int(Ans/2),2

A full program that is called like 5:prgmNAME.

TI-Basic is a tokenized lanugage, all tokens used here are one byte, except for remainder( which is two. represents the regative token, which is typed with the (-) key.

Examples:

0:prgmNAME
 => 0
1:prgmNAME
 => 1
2:prgmNAME
 => -1
#etc

Explanation:

‾int(‾Ans/2)(1-2remainder(int(Ans/2),2
‾int(‾Ans/2)                           # -int(-X) is ciel(X), so ciel(Ans/2)
                          int(Ans/2)   # int(X) is floor(X), so floor(Ans/2)
                remainder(int(Ans/2),2 # 1 if floor(Ans/2) is odd else 0
            (1-2remainder(int(Ans/2),2 # -1 if floor(Ans/2) is odd, else 1
_int(_Ans/2)(1-2remainder(int(Ans/2),2 # -ciel(Ans/2) if floor(Ans/2) is odd, else ciel(Ans/2)

Same formula as a Y-var function:

Y1= ‾int(‾X/2)(1-2remainder(int(X/2),2

pizzapants184

Posted 2017-09-15T15:20:49.087

Reputation: 3 174

2

Java 8, 15 bytes

n->~n/2*~-(n&2)

EDIT: Is Java really the shortest of the non-golfing languages?! o.Ô

Explanation:

Try it here.

I'll use the table below as reference of what's happening.

  1. ~n is equal to -n-1.
  2. Since integer division in Java automatically floors on positive integers and ceils on negative integers, ~n/2 will result in the sequence 0,-1,-1,-2,-2,-3,-3,-4,-4,-5,-5,...
  3. n&2 will result in either 0 or 2, in the sequence 0,0,2,2,0,0,2,2,0,0,2,...
  4. ~-x is equal to (x-1), so ~-(n&2) (((n&2)-1)) results in the sequence -1,-1,1,1,-1,-1,1,1,-1,-1,1,...
  5. Multiplying the two sequences of ~n/2 and ~-(n&2) gives is the correct sequence asked in the challenge: 0,1,-1,-2,2,3,-3,-4,4,5,-5,...

Overview table:

n       ~n      ~n/2    n&2     ~-(n&2)     ~n/2*~-(n&2)
0       -1      0       0       -1          0
1       -2      -1      0       -1          1
2       -3      -1      2       1           -1
3       -4      -2      2       1           -2
4       -5      -2      0       -1          2
5       -6      -3      0       -1          3
6       -7      -3      2       1           -3
7       -8      -4      2       1           -4
8       -9      -4      0       -1          4
9       -10     -5      0       -1          5
10      -11     -5      2       1           -5

Kevin Cruijssen

Posted 2017-09-15T15:20:49.087

Reputation: 67 575

2

Brain-Flak, 86 74 72 70 bytes

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

Try it online!

Explanation

There are two parts to this code. The first part

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

does the brawn of the computation. It determines ceil(n/2) and whether or not to negate the output.

To explain how it works I will first explain how one would calculate ceil(n/2). This could be done with the following code

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

This counts down from n each time it performs a not (([{}]())) on a counter and adds the counter to a result. Since the counter is zero half the time we only increment every other run starting with the first one.

Now I want to also compute the sign of our results. To do this we start another counter. This counter only changes state if the first counter is off. That way we get the desired pattern. We put these two counters on the off stack to ease with moving them around when the time comes.

Now once we have finished that computation our stack looks like this

          parity(n)
ceil(n/2) sign

So we need to do some work to get the intended result this second part does it.

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

Post Rock Garf Hunter

Posted 2017-09-15T15:20:49.087

Reputation: 55 382

1

Perl 5, 32 + 1 (-p) = 33 bytes

$_='-'x(($_+++($b=0|$_/2))%2).$b

Try it online!

Xcali

Posted 2017-09-15T15:20:49.087

Reputation: 7 671

1

Proton, 23 bytes

n=>-~n//2//(-1)**(n//2)

Try it online!

Port of Halvard's solution.

Proton, 23 bytes

n=>-~n//2*(-1)**(n%4>1)

Try it online!

Port of Leaky's solution.

A bit more Protonic, 24 bytes:

n=>-~n//2*(**)(-1,n%4>1)

Mr. Xcoder

Posted 2017-09-15T15:20:49.087

Reputation: 39 774

1

QBIC, 27 26 bytes

g=(:+1)'\2`~(a-g)%2|?-g\?g

Explanation

g=          set worker var 'g' to
(:+1)           our index (plus one for the ceil() bit)
'\2`            integer divided by 2 (the int div needs a code literal: '..`
~(a-g)%2    IF index - temp result is odd (index 2 minus result 1 = 1)
|?-g        THEN PRINT g negated
\?g         ELSE PRINT g

steenbergh

Posted 2017-09-15T15:20:49.087

Reputation: 7 772

1

Clojure 122 bytes

Verbose, even when golfed. I'm going for the sympathy vote here... :-)

Golfed:

(defn d[n](let[x(int(Math/ceil(/ n 2)))y(cond(or(and(even? n)(even? x))(and(odd? n)(odd? x)))(Math/abs x):else(- 0 x))]y))

Ungolfed:

(defn dizzy-integer [n]
  (let [x   (int (Math/ceil (/ n 2)))
        y   (cond
                (or (and (even? n) (even? x))
                    (and (odd? n)  (odd? x))) (Math/abs x)
                :else (- 0 x)) ]
    y))

Bob Jarvis - Reinstate Monica

Posted 2017-09-15T15:20:49.087

Reputation: 544

1

Excel VBA 32-Bit, 39 37 Bytes

Anonymous VBE immediate window function that takes input from cell A1 and outputs to the VBE immediate window

?[Sign((-1)^Int(A1/2))*Int((A1+1)/2)]

Restricted to 32-Bit as A^Bis not valid in 64-Bit (A ^B is as close as can be achieved)

Taylor Scott

Posted 2017-09-15T15:20:49.087

Reputation: 6 709

Is the space between (-1) and ^[Int needed? – Pavel – 2017-09-23T23:57:45.330

@Pavel at least for the 64-Bit Version of Excel VBA, yes; But that said I swear that it does not for the 32-Bit Version, but alas I cannot test that on any of the hardware I have on hand – Taylor Scott – 2017-09-24T03:47:04.493

@Pavel - I've looked at it under a 32-Bit System (default install spec) and under that system the space is not required - I have restricted the solution to 32-Bit to take advantage of this – Taylor Scott – 2017-09-24T15:13:37.350

1Cool! You forgot to add in the corrected byte count though. – Pavel – 2017-09-24T17:11:56.730

Whoops, Thanks @Pavel - Its fixed now – Taylor Scott – 2017-09-24T17:14:24.390

1

Julia 0.6, 16 bytes

It's just the java solution except I need ÷ for integer division.

n->~n÷2*~-(n&2)

Try it online!

gggg

Posted 2017-09-15T15:20:49.087

Reputation: 1 715