Up and down, up and down

34

2

Challenge:

Given a positive integer input n, create a vector that follows this pattern:

0  1  0 -1 -2 -1  0  1  2  3  2  1  0 -1 -2 -3 -4 -3 -2 -1 ... ±(n-1) ±n

Or, explained with words: The vector starts at 0, and makes increments of 1 until it reaches the smallest odd positive integer that isn't part of the sequence, then it makes decrements until it reaches the smallest (in magnitude) even negative integer that isn't part of the sequence. It continues this way until n is reached. The sequence will end on positive n if n is odd, and negative n if n is even.

The output format is flexible.

Test cases:

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

You may choose to take the n zero-indexed. n = 1 would then give 0 1 0 -1 -2.

This is , so the shortest code in each language wins! Explanations are encouraged as always!

Stewie Griffin

Posted 2018-05-17T17:36:06.200

Reputation: 43 471

2

Relevant: OEIS A196199.

– Mr. Xcoder – 2018-05-17T17:55:37.220

Answers

10

R, 58 54 50 48 43 bytes

-2 bytes thanks to MickyT

function(n)diffinv(rep(1:n%%2*2-1,1:n*2-1))

Try it online!

function(n)
 diffinv(                           # take cumulative sum, starting at 0 of
             1:n%%2*2-1,            # a vector of alternating 1,-1
         rep(                       # repeated
                        1:n*2-1))   # 1, 3, 5, etc. times

Giuseppe

Posted 2018-05-17T17:36:06.200

Reputation: 21 077

8

Perl 6,  60  26 bytes

{flat {((1,-*...*)ZX*(-$++...0...$++)xx$_)}(),$_*($_%2||-1)}

Try it

{[...] (-1,-*...*)Z*0..$_}

Try it

Expanded:

{  # bare block lambda with implicit parameter $_

  [...]  # reduce using &infix:«...» (sequence generator)

          ( -1, -* ... * ) # (-1, 1, -1, 1 ... *)

      Z*                   # zip multiplied with

          0 .. $_          # range up to and including input
}

(-1,-*...*)Z*0..$_ generates the sequence 0 1 -2 3 -4 5

Brad Gilbert b2gills

Posted 2018-05-17T17:36:06.200

Reputation: 12 713

7

Python 2, 69 57 56 bytes

f=lambda n:[0][n:]or f(n-1)+range(-n,n+1)[::n%2*2-1][2:]

Try it online!

For each n up to the input the range(-n,n) (inclusive) is calculated, inverted when n is an even number, has the fist two numbers (after the inversion) removed, and then appended to the output.

Rod

Posted 2018-05-17T17:36:06.200

Reputation: 17 588

7

05AB1E, 9 7 bytes

Saved 2 bytes thanks to @Emigna

Ýā®sm*Ÿ

Try it online!

My first 05AB1E answer (I think), so I may be missing some tricks...

Explanation

Ý         # push range [0 ... n]   stack: [[0 ... n]]
 ā        # push range [1 ... len(prev)]  [[0 ... n], [1 ... n+1]]
  ®       # push value of register        [[0 ... n], [1 ... n+1], -1]
   s      # swap top two values           [[0 ... n], -1, [1 ... n+1]]
    m     # power                         [[0 ... n], [-1, 1, -1, 1, ...]]
     *    # multiply                      [[0, 1, -2, 3, -4, 5, ...]]
      Ÿ   # range interpolation           [[0, 1, 0, -1, -2, -1, ...]]

I have to thank @Dennis for the original usage of Ÿ, otherwise I may not probably would never have known about it...

ETHproductions

Posted 2018-05-17T17:36:06.200

Reputation: 47 880

Nice :)! I got ÝεDÈi®*}}Ÿ without checking, ā®sm is crazy smart haha. – Magic Octopus Urn – 2018-05-18T01:44:35.287

6

JavaScript (ES6), 56 bytes

f=(n,b=d=1,k=0)=>[k,...k-d*n?f(n,k-b?b:(d=-d)-b,k+d):[]]

Try it online!

Commented

f = (               // f = recursive function taking:
  n,                //   n = input
  b =               //   b = boundary value, initialized to 1
  d = 1,            //   d = current direction, initialized to 1
  k = 0             //   k = current sequence value, initialized to 0
) =>                //
  [                 // update the sequence:
    k,              //   append the current value
    ...k - d * n ?  //   if |k| is not equal to |n|:
      f(            //     append the (spread) result of a recursive call:
        n,          //       use the original input
        k - b ?     //       if k has not reached the boundary value:
          b         //         leave b unchanged
        :           //       else:
          (d = -d)  //         reverse the direction
          - b,      //         and use a boundary of higher amplitude and opposite sign
        k + d       //       update k
      )             //     end of recursive call
    :               //   else:
      []            //     stop recursion and append nothing
  ]                 // end of sequence update

Arnauld

Posted 2018-05-17T17:36:06.200

Reputation: 111 334

6

05AB1E, 15 14 bytes

ÝDÉ·<*Ý€û˜ÔsF¨

Try it online!

Explanation

Ý                # push range [0 ... n]
 D               # duplicate
  É·<            # (x % 2 == 1)*2-1 for each
     *           # multiply
      Ý          # range [0 ... a] for each
       €û        # palendromize each
         ˜       # flatten
          Ô      # connected uniqueified
           sF¨   # remove the last n elements

Emigna

Posted 2018-05-17T17:36:06.200

Reputation: 50 798

6

Jelly, 11 9 bytes

²Ḷƽ-*0;Ä

Try it online!

How it works

²Ḷƽ-*0;Ä  Main link. Argument: n

²          Square; yield n².
 Ḷ         Unlength; yield [0, ..., n²-1].
  ƽ       Take the integer square root of each k in the range.
    -*     Compute (-1)**r for each integer square root r.
      0;   Prepend a zero.
        Ä  Accumulate; take the sums of all prefixes.

Dennis

Posted 2018-05-17T17:36:06.200

Reputation: 196 637

6

Haskell, 48 42 bytes

f n=0:[(-1)^i*x|i<-[0..n-1],x<-[1-i..i+1]]

Try it online!

Thanks to Οurous for -1 byte

Even though it's kind of obvious in hindsight, it took me a while to arrive at (-1)^i*x which is x when i is even and -x when i is odd. Previous iterations where:

(-1)^i*x
x-2*mod i 2*x
(-1)^mod i 2*x
[x,-x]!!mod i 2
(1-sum[2|odd i])*x

Laikoni

Posted 2018-05-17T17:36:06.200

Reputation: 23 676

1You can save a byte by using 1-i instead of -i+1 in the .. expression. – Οurous – 2018-05-18T00:06:10.573

6

Haskell, 43 bytes

f n=scanl(-)0[(-1)^k|k<-[1..n],_<-[2..2*k]]

Try it online!

Computes the negated cumulative sums of the list [(-1)^k|k<-[1..n],_<-[2..2*k]], which is the first n rows of

[-1,
 +1, +1, +1,
 -1, -1, -1, -1, -1,
 +1, +1, +1, +1, +1, +1, +1…

Lynn

Posted 2018-05-17T17:36:06.200

Reputation: 55 648

4

J, 25 bytes

-5 bytes thanks to FrownyFrog!

>:@*:$i.;@(<@i:@*_1&^)@,]

Try it online!

J, 30 bytes

>:@*:{.;@([:(i:@*_1&^)&.>i.,])

Explanation:

i.,] creates list 0..n

&.> for each number in the list execute the verb in (...) and box the result (I need boxing because the results have different length)

[:( _1&^) find -1 to the ith power (-1 or 1)

i:@* make a list -n..n or n..-n, depending on the sign of the above

;@ unbox

>:@*: find n^2 + 1

}. and take so many numbers from the list

Try it online!

Galen Ivanov

Posted 2018-05-17T17:36:06.200

Reputation: 13 815

1would you consider writing the same code as a zero based n version? e.g *:{.;@([:(i:@*_1&^)&.>i.) .. the specification allows that – jayprich – 2018-05-28T16:37:15.280

"n = 1 would then give 0 1 0 -1 -2" – FrownyFrog – 2018-05-30T02:34:14.300

@FrownyFrog - Hmm, I didn't check it. I reverted to my first solution. Thank you for the observation! – Galen Ivanov – 2018-05-30T07:20:28.813

1

25

Use $ for the cut-off, no need for &.> because * is rank-0.

– FrownyFrog – 2018-05-30T14:59:34.537

4

C# (.NET Core), 300 167 bytes

I've never done any of these before, but this one seemed fun. I see why people use those "golfing" languages as 167 seems way higher than some of the other answers. But, you gotta go with what you know.

static int[] f(int n){if (n==1) return new int[]{0,1};var a=f(n-1);return a.Concat(a.Skip(a.Length-(n-1)*2).Select(x=>-x)).Concat(new int[]{(n%2)!=0?n:-n}).ToArray();}

Try it online!

// Recursive Worker Function
static public int[] f( int n )
{
    // Start with the simple case
    if ( n == 1 ) return new int[]{0,1};

    // Recusively build off of that
    var a = f(n-1);

    // To be added at the end
    int[] b = { (n%2) !=0 ? n : -n };

    // Skip some based on length
    int s = a.Length - (n-1)*2;

    // With the rest, multiply by -1 and then append to the end
    // And append the part
    return a.Concat( a.Skip(s).Select( x => -x ) ).Concat( b ).ToArray();
}

Darrin Cullop

Posted 2018-05-17T17:36:06.200

Reputation: 141

1You can make this a lot shorter if you only count the using statements and the function. This is allowed by default unless the challenge specifies it must be a full program (even if it did, you could shorten the containing class name). – Οurous – 2018-05-18T00:20:48.717

Thank you! Thanks to your suggestion, I figured out the meaning of the "header" and "footer" sections of the TIO site. That cut my submission size in half! – Darrin Cullop – 2018-05-18T00:32:47.773

2Welcome to PPCG! (this looks like your first post.) Don't worry about the other languages, just try to be as good as possible in your language. / Tips: Remove unnecessary spaces. In C# you can remove all spaces surrounding symbols ([](){};.) (n-1)*2 is just 2*n-2 and with some rearrangement you can remove the parentheses there. – user202729 – 2018-05-18T04:37:16.993

Besides, != has precedence less than % so you can remove a pair of parens. And you can use >0 instead of `!=0, saves a byte. – user202729 – 2018-05-18T04:38:51.370

1

Also from me: welcome to PPCG! Tips for golfing in C# and Tips for golfing in all languages might be interesting to read through. :) As for some golfing tips: static int[] f(int n) can become f=n=> by using a (recursive) lambda, and (n-1)*2 can become ~-n*2 to save on the parenthesis. I got it down to 155 (137 + 18) bytes: Try it online. The 18 bytes are for using System.Linq;, because required imports are mandatory for the byte-count. Enjoy your stay!

– Kevin Cruijssen – 2018-05-18T08:00:43.703

3

Python 2, 65 56 bytes

r=k=0;exec'print r;r+=1-k**.5//1%2*2;k+=1;'*-~input()**2

The output format is a bit ugly. :/

Try it online!

Dennis

Posted 2018-05-17T17:36:06.200

Reputation: 196 637

3

Java 8, 85 83 79 bytes

n->{for(int p=0,i=0;i<=n*n;p+=1-(int)Math.sqrt(i++)%2*2)System.out.println(p);}

-6 bytes thanks to @OlivierGrégoire.

Try it online.

Explanation:

n->{                            // Method with integer parameter and no return-type
  for(int p=0,                  //  Set both `p` to 0
      i=0;i<=n*n;               //  Loop `i` in the range [0, `n*n`]
      p+=                       //    After every iteration, increase `p` by:
         1-                     //     1, minus:
           (int)Math.sqrt(i++)  //     The square-root of `i`, truncated to its integer
           %2*2)                //     Modulo 2, and multiplied by 2
     System.out.println(p);}    //   Print integer `p` with a trailing new-line

Kevin Cruijssen

Posted 2018-05-17T17:36:06.200

Reputation: 67 575

Nice approach. I was working on such approach right now, to improve my answer, but you beat me to it (despite your meeting), well done! ;-) – Olivier Grégoire – 2018-05-18T09:12:37.037

183 bytes (I just removed j). – Olivier Grégoire – 2018-05-18T09:15:21.977

179 bytes: I made i go up instead of down to remove a redundant n*n. – Olivier Grégoire – 2018-05-18T11:27:52.407

Hi. Writing this to inform me you that I basically ripped off your answer. (port to JavaScript). Hope it's ok

– Muhammad Salman – 2018-05-20T19:01:12.037

@MuhammadSalman Sure, np. I port answers from others pretty often as well. :) As long as the original answer is mentioned, like you did, it's all fine by me. – Kevin Cruijssen – 2018-05-20T20:29:39.420

3

R, 48 46 42 bytes

for(i in 1:scan())F=c(F,-(-1)^i*(2-i):i);F

Try it online!

A port of the Ruby answer by Kirill L. - and saved 6 bytes thanks to the same Kirill L.! Now shorter than Giuseppe's solution ;)

A port of this Octave answer by Luis Mendo using approx is less golfy. n=n^2+1 can be replaced by ,,n^2+1; or by 0:n^2+1(positional argument xout) for the same byte count :

R, 56 bytes

f=function(n)approx((0:n)^2+1,-(-1)^(0:n)*0:n,n=n^2+1)$y

Try it online!

JayCe

Posted 2018-05-17T17:36:06.200

Reputation: 2 655

I think approx will work here in a similar manner to Luis Mendo's Octave solution as well. – Giuseppe – 2018-05-19T00:12:43.043

@Giuseppe Thanks! It does work though it's longer. I've learned diffinv and approx from this question... – JayCe – 2018-05-20T00:40:28.313

Although I also don't know a golfier way to do -1 power (in R ~ doesn't work as a complement operator :(), you can still save another 2 bytes by switching to a full program.

– Kirill L. – 2018-05-20T11:52:12.783

...and since it is a full program we can also use and spoil a predefined built-in: 42 bytes - finally, shorter than Giuseppe's!

– Kirill L. – 2018-05-20T13:33:27.917

3

APL (Dyalog Unicode), 17 bytes

+\0,¯1*⍳(/⍨)1+2×⍳

Try it online!

Golfed 2 bytes thanks to @FrownyFrog by converting to a train. See the older answer and its explanation below.


APL (Dyalog Unicode), 19 bytes

+\0,∊⊢∘-\⍴∘1¨1+2×⍳⎕

Try it online!

(Uses ⎕IO←0)

My first approach was to construct multiple ranges and concatenate them together, this easily went over 30 bytes. Then I started analysing the sequence

      +\⍣¯1⊢0  1  0 ¯1 ¯2 ¯1  0  1  2  3  2  1  0 ¯1 ¯2 ¯3 ¯4
0 1 ¯1 ¯1 ¯1 1 1 1 1 1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1

+\⍣¯1 denotes the inverse cumulative sum

There is a repeating pattern of 1s and ¯1s, where the length of each consecutive sequence of 1s or ¯1s is 1+2×n. And each subsequence alternates between 1 and ¯1. What I can do now is to create the 1s and ¯1s list, and then scan by +

      ⍳4 ⍝ creates range 0..4
0 1 2 3
      2×⍳4
0 2 4 6
      1+2×⍳4
1 3 5 7
      ⍴∘1¨1+2×⍳4 ⍝ for-each create that many 1s
┌─┬─────┬─────────┬─────────────┐
│1│1 1 1│1 1 1 1 1│1 1 1 1 1 1 1│
└─┴─────┴─────────┴─────────────┘
      ⊢∘-\⍴∘1¨1+2×⍳4 ⍝ alternate signs
┌─┬────────┬─────────┬────────────────────┐
│1│¯1 ¯1 ¯1│1 1 1 1 1│¯1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1│
└─┴────────┴─────────┴────────────────────┘
      ∊⊢∘-\⍴∘1¨1+2×⍳4 ⍝ flatten
1 ¯1 ¯1 ¯1 1 1 1 1 1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1
      0,∊⊢∘-\⍴∘1¨1+2×⍳4
0 1 ¯1 ¯1 ¯1 1 1 1 1 1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1
      +\0,∊⊢∘-\⍴∘1¨1+2×⍳4 ⍝ cumulative sum
0 1 0 ¯1 ¯2 ¯1 0 1 2 3 2 1 0 ¯1 ¯2 ¯3 ¯4

user41805

Posted 2018-05-17T17:36:06.200

Reputation: 16 320

Checking other answers now, I see many also use the +\ method, but generate the sequence of 1s and ¯1s with ¯1⌊.5⍨×⍨⍳ which happens to be at least 3 bytes shorter. – user41805 – 2018-05-20T17:50:48.570

+\0,¯1*⍳(/⍨)1+2×⍳ is 17 – FrownyFrog – 2018-05-20T18:57:57.467

I knew my solution felt long – Zacharý – 2018-05-20T23:15:00.027

2

Haskell, 47 bytes

(#0)
m#n|m>n=[-n..n]++map(0-)(m#(n+1))|1>0=[-m]

Try it online!

user1472751

Posted 2018-05-17T17:36:06.200

Reputation: 1 511

2

MATL, 17 15 bytes

-2 bytes thanks to Luis Mendo!

0i:oEqG:EqY"Ysh

Try it online!

Explanation for n=3:

0		% push 0
 i:		% read input as integer, push range
		% stack: [0, [1 2 3]]
   o		% modulo 2, stack: [0, [1 0 1]]
    Eq		% double and decrement, stack: [0, [1 -1 1]]
      G:	% push input and range again
		% stack: [0, [1 -1 1], [1 2 3]]
        Eq	% double and decrement,
		% stack: [0, [1 -1 1], [1 3 5]]
	  Y"	% run-length decoding
		% stack: [0, [1 -1 -1 -1 1 1 1 1 1]]
	    Ys	% cumulative sum
		% stack: [0, [1  0 -1 -2 -1  0  1  2  3]]
	      h	% horizontally concatenate
		% end of program, automatically print the stack

Giuseppe

Posted 2018-05-17T17:36:06.200

Reputation: 21 077

2

Octave, 44 42 41 bytes

2 bytes removed thanks to @StewieGriffin, and 1 byte further removed thanks to @Giuseppe!

@(n)interp1((t=0:n).^2,-t.*(-1).^t,0:n^2)

Try it online!

Luis Mendo

Posted 2018-05-17T17:36:06.200

Reputation: 87 464

2

Java (JDK 10), 98 bytes

n->{var s="0";for(int i=0,r=0,d=1;i++<n;s+=" "+r,d=-d)for(r+=d;r!=i&r!=-i;r+=d)s+=" "+r;return s;}

Try it online!

Olivier Grégoire

Posted 2018-05-17T17:36:06.200

Reputation: 10 647

Ah, while I was in my meeting you sneaked in an answer before me.. ;) Will leave mine as well through, because we use a completely different approach. +1 either way. – Kevin Cruijssen – 2018-05-18T09:05:33.297

2

Ruby, 52 47 bytes

f=->n{n<1?[0]:f[n-1]+(2-n..n).map{|x|-~0**n*x}}

Try it online!

Below is the original 52-byte version with an explanation:

f=->n{n<1?[0]:f[n-1]+[(r=*2-n..n).map(&:-@),r][n%2]}

Try it online!

Walkthrough

f=->n{           #Recursive approach
 n<1?[0]         #Init with 0 if n=0
 :f[n-1]         #else make a recursive call
 +               #and append an array of numbers
 [(r=*2-n..n)    #Init r as splatted range from 2-n to n
 .map(&:-@)      #"-@" is unary minus, so this a fancy way to do map{|x|-x} for -1 byte
                 #For even n use this negated r, e.g. for n=4: [2, 1, 0, -1, -2, -3, -4]
 ,r]             #For odd n use r directly, e.g. for n=3: [-1, 0, 1, 2, 3]
 [n%2]           #Odd/even selector
}

Kirill L.

Posted 2018-05-17T17:36:06.200

Reputation: 6 693

I don't know Ruby - can you explain what this does especially the map(&:-@) portion? – JayCe – 2018-05-18T21:23:36.203

@JayCe Added an explanation. Basically, this is just negation, what in R would be simply -r. – Kirill L. – 2018-05-19T10:08:18.560

Thanks for the explanation - it helped me port this to R. – JayCe – 2018-05-19T16:50:16.513

2

Prolog (SWI), 113 bytes

0-I-[0|I].
N-[H|T]-R:-N is -H*(-1)^N,A is N-1,A-[H|T]-R;I is H-(-1)^N,N-[I|[H|T]]-R.
N-O:-X is -N*(-1)^N,N-[X]-O.

Try it online!

ASCII-only

Posted 2018-05-17T17:36:06.200

Reputation: 4 687

1

Python 3, 83 bytes

def c(n):print([(-1)**j*(abs(j-i)-j)for j in range(n+1)for i in range(2*j)][:-n+1])

bobrobbob

Posted 2018-05-17T17:36:06.200

Reputation: 160

1

Husk, 18 17 bytes

:ṁoṡ₁ŀ⁰_₁⁰
*^⁰_1⁰

Try it online!

user48543

Posted 2018-05-17T17:36:06.200

Reputation:

1

Charcoal, 19 bytes

F⊕NI×∨﹪ι²±¹…·∧ι⁻²ιι

Try it online! Link is to verbose version of code. Explanation:

  N                 Input as a number
 ⊕                  Increment
F                   Loop over implicit range
                ²   Literal 2
                 ι  Current index
               ⁻    Subtract
              ι     Current index
             ∧      Logical And
                  ι Current index
           …·       Inclusive range
       ι            Current index
        ²           Literal 2
      ﹪             Modulo
          ¹         Literal 1
         ±          Negate
     ∨              Logical Or
    ×               Multiply
   I                Cast to string and implicitly print

Alternative explanation:

F⊕N

Loop over the integers from 0 to the input inclusive.

Cast the results to string before printing.

×∨﹪ι²±¹

Negate alternate sets of results.

…·∧ι⁻²ιι

Form a list from the previous index to the current index, excluding the previous index.

Neil

Posted 2018-05-17T17:36:06.200

Reputation: 95 035

1

Jelly,  11  12 bytes

Bah, I thought I had 11 wih _2+ỊrN)N;¥/

_2+ỊrN×-*$)Ẏ

Try it online!

How?

_2+ỊrN×-*$)Ẏ - Main Link: n           e.g. 4
          )  - for x in [1...n]:           1       2          3               4
_2           -   subtract 2 from x        -1       0          1               2
   Ị         -   is x insignificant?       1       0          0               0
  +          -   add                       0       0          1               2
     N       -   negate x                 -1      -2         -3              -4
    r        -   inclusive range          [0,-1]  [0,-1,-2]  [1,0,-1,-2,-3]  [2,1,0,-1,-2,-3,-4]
         $   -   last two links as a monad:
       -     -     minus one              -1      -1         -1              -1
        *    -     raised to the power x  -1       1         -1               1
      ×      -   multiply                 [0,1]   [0,-1,-2]  [-1,0,1,2,3]    [2,1,0,-1,-2,-3,-4]
           Ẏ - tighten                    [0,1,0,-1,-2,-1,0,1,2,3,2,1,0,-1,-2,-3,-4]

Jonathan Allan

Posted 2018-05-17T17:36:06.200

Reputation: 67 804

1

APL (Dyalog Unicode), 34 32 bytes

⎕CY'dfns'⋄-0,∘∊1↓¨2to/Sׯ1*S←⍳,⊢

Try it online!

Requires ⎕IO←0

-2 bytes thanks to @FrownyFrog

Zacharý

Posted 2018-05-17T17:36:06.200

Reputation: 5 710

32 as a train – FrownyFrog – 2018-05-19T11:24:32.130

1

Stax, 10 bytes

╗)SΘ█☼₧ΘP(

Run and debug it

recursive

Posted 2018-05-17T17:36:06.200

Reputation: 8 616

1

Scala, 119 Bytes

def a(n: Int)={lazy val s:Stream[Int]=0#::Stream.from(0).map{x=>s(x)+1 -2*(Math.sqrt(x).toInt%2)}
s.take(n*n+1).toList}

Ungolfed:

def a(n: Int)={
  lazy val s:Stream[Int]= 0#::Stream.from(0).map //Give the starting point and indexing scheme
  {
    x=>
    {
      val sign = 1-2*(Math.sqrt(x).toInt%2) //Determine whether we are adding or subtracting at the current index
      s(x)+sign
    }
  }
  s.take(n*n+1).toList //Take the desired values
}

This can probably be golfed much better, but I wanted a solution utilizing lazy Streams.

Ethan

Posted 2018-05-17T17:36:06.200

Reputation: 271

1

Stacked, 44 bytes

[~>0\:2%\#,2*1-tr[...rep]flatmap,$sumonpref]

Try it online! It's been a while since I programmed in Stacked, but I think I still got it.

Alternatives

73 bytes: [0\|>:2%tmo*2 infixes[:...|>\rev...|>rev#,$#'sortby 1#behead]flatmap 0\,]

This goes with the "ranges from generated indices" approach used in my Attache answer. This proved to be pretty long, since Stacked has no builtin for reversed ranges nor collapsing. (That's what :...|>\rev...|>rev#,$#'sortby 1#behead does.)

53 bytes: [0\|>:2%tmo _\tpo#,tr[...rep]flatmap 0\,inits$summap]

...so I decided to go for an approach which instead finds the cumulative sum (inits$summap) over 1 and -1 repeated by the odd integers, as in the R answer.

46 bytes: [~>0\:2%\#,2*1-tr[...rep]flatmap,inits$summap]

...but I realized that the negative integers and the odd integers could be made in one go, by multiplying both generated arrays (the mod 2 values of the range and the range itself) by 2 then subtracting 1. This gives alternating 1s and -1s for the first range and the odd integers for the second!

44 bytes: [~>0\:2%\#,2*1-tr[...rep]flatmap,$sumonpref]

... and then I remembered I had a builtin for mapping prefixes. ^-^

Conor O'Brien

Posted 2018-05-17T17:36:06.200

Reputation: 36 228

1

Julia 0.6, 44 bytes

n->[(i%2*2-1)*[0:i;(n>i)*~-i:-1:1]for i=1:n]

Try it online!

Since OP mentions "the output format is flexible", this prints an array of sub arrays, eg. U(3) => [[0, 1], [0, -1, -2, -1], [0, 1, 2, 3]].

i%2*2-1 decides the sign of the current subarray - negative for even numbers, positive for odd.

[0:i;(n>i)*~-i:-1:1] is in two parts. 0:i is straightforward, the range of values from 0 to the current i. In the next part, ~-i:-1:1 is the descending range from i-1 to 1. But we want to append this only if we're not yet at the final value, so multiply the upper end of the range by (n>i) so that when n==i, the range will be 0:-1:1 which ends up empty (so the array stops at n correctly).


And here's a version that can support random access - the inner lambda here returns the i'th term of the sequence without having to have stored any of the terms before it. This one gives the output as a single neat array too.

49 47 bytes

n->map(i->((m=isqrt(i))%2*2-1)*(m-i+m^2),0:n^2)

Try it online!

sundar - Reinstate Monica

Posted 2018-05-17T17:36:06.200

Reputation: 5 296

0

JavaScript (Node.js), 83 bytes

n=>[v=0].concat(...[...Array(n),c=d=-1].map(t=>[...Array(c+=2)].map(t=>v+=d,d=-d)))

Try it online!

l4m2

Posted 2018-05-17T17:36:06.200

Reputation: 5 985

0

Jelly, 17 16 15 bytes

-*N׌Ḅ)j00;ḣN‘$

Try it online!

dylnan

Posted 2018-05-17T17:36:06.200

Reputation: 4 993

0

Wolfram Language (Mathematica), 56 51 bytes

-5 bytes thanks to user202729

Join@@MapAt[-#&,Table[Range[-k,k],{k,#}],1;;-1;;2]&

Try it online!

chuy

Posted 2018-05-17T17:36:06.200

Reputation: 389

Flatten@ can be Join@@. Table can use prefix notation. Range can be shortened by calculating the reverse range and then negate. – user202729 – 2018-05-18T04:27:45.077

0

Clean, 59 bytes

import StdEnv
$m=foldr(\a=(++)[~a..a]o map~)[][0..m-1]++[m]

Try it online!

Defines the function $ :: Int -> [Int] generating the required list.

A direct port of user1472751's Haskell solution ended up being slightly better:

Clean, 53 bytes

import StdEnv
$n m|m>n=[~n..n]++map~($(n+1)m)=[~m]

$0

Try it online!

Οurous

Posted 2018-05-17T17:36:06.200

Reputation: 7 916

0

Ruby, 59 bytes

->n{a,*b=-1;n.times{|z|b+=[*(z*a).step(z*a=-a,a)]};b+[n*a]}

Try it online!

G B

Posted 2018-05-17T17:36:06.200

Reputation: 11 099

0

C# (.NET Core), 97 bytes

n=>{var r="";int m=0,a=0,i=0;for(;++i<=n;)for(m=i%2>0?1:-1;a!=i*m;a+=m)r+=a+" ";r+=n*m;return r;}

Try it online!

Another different approach in C#.

Charlie

Posted 2018-05-17T17:36:06.200

Reputation: 11 448

0

CJam, 59 bytes

1]q~:X*ee{~[\_-1\#@*](2*)*}%:+W%~[]\_@+{@@+_@+}XX*(*W%0\+p;

Try it online!

Suggestions for improvement are welcome!

EDIT: forgot leading zero

maxb

Posted 2018-05-17T17:36:06.200

Reputation: 5 754

0

Python 3, 83 bytes

lambda n:numpy.cumsum([0]+sum([[(-1)**i]*(i-~i)for i in range(n)],[]))
import numpy

Try it online!

I'll throw in a numpy solution, just for fun.

maxb

Posted 2018-05-17T17:36:06.200

Reputation: 5 754

0

Swift, 293 bytes

let odd = n % 2 != 0
var i = 0
var inc = 1
var s : [Int] = [0]
repeat {
    i += inc
    if (inc == 1 && i % 2 != 0) || (inc == -1 && i % 2 == 0) {
        if !s.contains(i) {
            inc = -inc
        }
    }
    s.append(i)
} while !((odd && i == n) || (!odd && i == -n))
print("\(s)")

onnoweb

Posted 2018-05-17T17:36:06.200

Reputation: 211

4Welcome to PPCG! This isn't currently a valid submission as it is neither a full program nor a function (it assumes n is a predefined variable, which isn't allowed by default), and you can save a lot of bytes by shortening variable names and removing white-space. – Οurous – 2018-05-19T02:42:21.720

0

Attache, 38 bytes

Collapse@@{`::&>Pairs!On[Even,`-,0:_]}

Try it online!

I'm struggling to break 38 bytes... I'll post an explanation once I'm convinced I can't get lower than this (or if I golf it!).

Conor O'Brien

Posted 2018-05-17T17:36:06.200

Reputation: 36 228

0

JavaScript (Node.js), 51 bytes

n=>{for(p=i=0;i<=n*n;p+=1-(i++**.5|0)%2*2)alert(p)}

Try it online!

I stole toke ripped off used @KevinCruijssen's answer.

Thanks to @StewieGriffin for adding one more byte to my count (I forgot to output the last number) read the comments

Muhammad Salman

Posted 2018-05-17T17:36:06.200

Reputation: 2 361

0

Yabasic, 83 bytes

An anonymous function that takes Input, n as an integer and outputs to the console.

Input""n
For i=1To n
For j=-i To i-1
k=i-Abs(j)
?(-1)^(i+1)*k;
If k=n End
Next
Next

Try it online!

Taylor Scott

Posted 2018-05-17T17:36:06.200

Reputation: 6 709