g | x | w | all
Bytes Lang Time Link
085TIBASIC TI83 Plus250331T163725Zmadeforl
972jBasher2250206T191808Zmadeforl
096CASIO BASIC CASIO fx9750giii250123T185554Zmadeforl
088250123T155653ZUsed_Bra
073Ruby250122T082950ZG B
109Maple250122T184104ZSophia A
018Japt250119T231154ZShaggy
106SageMath250121T201255ZSophia A
076JavaScript ES7250119T171441ZArnauld
075R250119T231755ZDominic
091Python 3.8250119T184531ZJonathan
021Jelly250119T154250ZJonathan
074Charcoal250119T152622ZNeil

TI-BASIC (TI-83 Plus), 85 Bytes

1→I
Input B
B→dim(L₁
1→L₁(1
1→E
While B>E
If max(L₁=I)+fPart(√(abs(L₁(E)-I
Then
(I<0)-I→I
Else
E+1→E
I→L₁(E
1→I
End
End
L₁

translation of my casio basic answer

jBasher2, 972 bytes

this probably could be made better, i'm just lazy.

suprisingly a translation of my answer in casio basic

create i with type number
create n with type number
create b with type number
create l with type list
create t with type number
create u with type number
create k with type number
create j with type number
set 1 to i
set 1 to n
ask for input
set that to b
push 0 to l
push 1 to that
set that to l
while n < b
push 0 to l
set that to l
add n by 1
set that to n
endwhile
set 1 to t
while b > t
create y with type list
set 0 to n
get length of l
set that to u
while n < u
set 0 to k
get item from l at n
if that == i
set 1 to k
endif
push k to y
set that to y
add n by 1
set that to n
endwhile
get item from l at t
subtract that by i
get the absolute value of that
get the square root of that
get the fractional part of that
set that to j
get the maximum value in y
add that by j
set that to j
if j > 0
spawn 0
if i < 0
spawn 1
endif
subtract that by i
set that to i
endif
if j == 0
add t by 1
set that to t
change item in l at index t to i
set 1 to i
endif
endwhile
output l

CASIO BASIC (CASIO fx-9750giii), 96 bytes

1→I
?→B
B→Dim List1
1→List1[1]
1
While B>Ans
List1=I
If Max(List Ans)+Frac √Abs (List1[Ans]-I
Then (I<0)-I→I
Else Ans+1
I→List1[Ans
1→I
IfEnd
WhileEnd
List1

it gets slow really quickly, but it works

I could probably make it smaller if I can find an equation for it, but idk how

☾, 34 characters, 88 bytes

code with custom font

󷺹⭥ſ⁰􋄎󷺹+⛶←󷺹<1→-⟞ᐸ⊚ᐳ√⫰(⟞􍨄-⟝)%1󷺥⟞∋󷺽∘0

(Note: ☾ uses a custom font, you can copy-paste the above code into the Web UI)

We first need a function that maps 0 to 1 to -1 to 2 etc.: first function

We need a function to check that a number works provided there is a separate variable with all past elements that we need to avoid. The list is the left-tack and the function's argument is the right-tack: second function

These combine into a function that finds the first successful integer provided the list. function combination

We start with any list with b things; the fold only has a 1-variable function as its second argument. The shortest way of doing that is with range. Initialize with [0], and the previous functions take care of generating the sequence. final function

Ruby, 86 ... 73 bytes

->n,*r{n.times{r<<n=([0,n+1]+r.map{|b|n-b*b.abs}-r).min_by{|x|x**4-x}};r}

Try it online!

Today I learned: sum([]) is the same as flat_map only 1 byte shorter.

(EDIT: then I found a different way and saved 8 bytes.)

Why?

First, "bootstrap" the sequence, by adding 0 and 1. Reuse n, the first iteration will set it to zero, and the next n+1 will be 1 and 2.

Now, we can search inside the sequence, given enough seeds, b*b.abs can generate squares and make them positive and negative. This will get us the next number already (2-2*2.abs -> -2). But we can't continue from there yet, so the next step will be n+1 again, which will add -1. From now on, the sequence will feed itself.

x**4-x checks absolute value first and then in case of a tie decides for the positive value.

Maple, 112 109 bytes

f:=(b,a,i)->`if`(b=1,a,`if`(issqr(abs(a[-1]-i))and not(i in a),f(b-1,[op(a),i],1),f(b,a,`if`(i<0,1-i,-i)))):

A recursive function \$f(b,[0],1)\$ that for \$b\$ returns a list of integers \$a(n) \$ for \$\forall n \in [0, b]\$.

Japt, 19 18 bytes

Both versions output the first n terms. Replace h with g to output the 0-based n-th term.

This first version works on the basis that n is always >=a(n), so the min method will, therefore, always return a(n).

@ËaX ¬v1 «ZøD}cn}h

Try it

@ËaX ¬v1 «ZøD}cn}h     :Implicit input of integer U
@                      :Outer function taking an integer (X) and an array (Z) as arguments
 Ë                     :  Minimum of U and the following inner function, taking an integer D as argument
  aX                   :    Absolute difference between D & X
     ¬                 :    Square root
      v1               :    Divisible by 1?
         «             :    Logical AND with logical NOT of
          ZøD          :      Z contains D
             }         :  End inner function
              c        :  Get the first integer in the sequence [0,-1,1,-2,2,-3,3,...] that returns true
                       :   when passed through the inner function
               n       :    After first negating it (to instead give the sequence [0,1,-1,2,-2,3,-3,...])
                }      :End outer function
                 h     :Starting with [0,1], pass the current last element (X) and the array (Z) through the outer function,
                       : pushing the result to the array until it reaches length U

Original, 19 bytes

T@_aT ¬v1 «YøZ}cn}h

Try it

T@_aT ¬v1 «YøZ}cn}h     :Implicit input of integer U
T@                      :Outer function taking an integer (T) and an array (Y) as arguments
  _                     :  Inner function taking an integer Z as argument
   aT                   :    Absolute difference with T
      ¬                 :    Square root
       v1               :    Divisible by 1?
          «             :    Logical AND with logical NOT of
           YøZ          :      Y contains Z
              }         :  End inner function
               c        :  Get the first integer in the sequence [0,-1,1,-2,2,-3,3,...] that returns true
                        :   when passed through the inner function
                n       :    After first negating it (to instead give the sequence [0,1,-1,2,-2,3,-3,...])
                 }      :End outer function
                  h     :Starting with [0,1], pass the current last element (T) and the array (Y) through the outer function,
                        : pushing the result to the array until it reaches length U

The T at the start is necessary here as, without it, the parameters of the outer function would be X (the current integer), Y (the iteration index) & Z (the array). But the Z would be overridden by the inner function which has a single parameter, also named Z, which creates a problem as we need access to the array in the inner function.

Thankfully (for once) through a quirk, bug, or perhaps even a feature of Japt, any variable name (uppercase letter) preceding a function declaration shortcut (@, in this case) gets prepended to that function's list of parameters. For some reason that I've yet to fathom, T (which normally defaults to 0) is the only variable that works in this instance - I would have expected any letter that isn't U, X, Y or Z to also work, most especially V as it also defaults to 0. So, T is now the current integer, X the iteration index, Y the array, and Z is null, allowing us to use parameter Z in the inner function while still being able to access the array.

SageMath, 106 bytes

f=lambda b,a=[0],i=1:a if b<1 else f(b,a,(i<0)-i)if i in a or not is_square(abs(a[-1]-i))else f(b-1,a+[i])

A recursive function that accepts \$b\$ and returns a list of integers \$a(n) \$ for \$\forall n \in [0, b]\$.

Try it online!

JavaScript (ES7), 76 bytes

Takes an integer \$n\$ and returns the \$n\$-th term (1-indexed).

n=>(v=g=_=>n?g(g[(h=i=>g[i]||((i-v)**2)**.25%1?h((i<1)-i):v=i)(0)]=n--):v)()

Try it online!

Commented

n => (                   // n = input
  v =                    // v = previous value in the sequence
  g = _ =>               // g is a recursive function, whose underlying
                         // object is re-used to store the sequence values
  n ?                    // if n is not zero:
    g(                   //   do a recursive call to g:
      g[                 //     update g[h(0)], where ...
        (                //
          h = i =>       //     ... h is a recursive function taking i:
          g[i] ||        //       if g[i] is defined
          ((i - v) ** 2) //       or the squared difference between i and v
          ** .25 % 1 ?   //       is not the 4th power of an integer:
            h(           //         do a recursive call to h:
              (i < 1)    //           with i updated as follows:
              - i        //           0 -> 1 -> -1 -> 2 -> -2 -> ...
            )            //         end of recursive call
          :              //       else:
            v = i        //         save the new value i in v
        )(0)             //     initial call to h
      ] = n--            //     set g[h(0)] and decrement n
    )                    //   end of recursive call
  :                      // else:
    v                    //   return v
)()                      // initial call to g

JavaScript (ES7), 84 bytes

Takes an integer \$n\$ and returns the first \$n\$ entries of the sequence.

n=>(v=g=_=>n?[(h=i=>g[i]||((i-v)**2)**.25%1?h((i<1)-i):v=i)(0),...g(g[v]=n--)]:[])()

Try it online!

R, 75 bytes

repeat{b=0;while(abs(b-F[1])^.5%%1|b%in%F)b=-b+!b>0;F=c(b,F);cat(F[2],"
")}

Try it online!

Prints the infinite sequence.


R, 79 (or 76) bytes

\(n){while(n<-n-1){b=0;while(abs(b-F[1])^.5%%1|b%in%F)b=-b+!b>0;F=c(b,F)};F[1]}

Attempt This Online!

Outputs the n-th element of the sequence.
76 bytes by outputting the sequence up to the n-th element, but in reverse order.

Python 3.8, 91 bytes

f=lambda b,r=[0],c=1:r*(r[:b]<r)or f(b,r+[c]*(v:=abs(r[-1]-c)**.5%1==(c in r)),v or(c<0)-c)

A recursive function that accepts \$b\$ and returns a list of integers \$a(n) \forall n \in [0,b]\$.

Try it online!

Jelly,  23  21 bytes

-2 thanks to Unrelated String (0ị$ -> ṛ/ and inlining using an initial niladic chain to use the default argument of zero)

µ²Ṁ‘rN$AÞḟạƲ¥Ƈṛ/Ḣṭø¡

A full program that accepts a non-negative integer, \$b\$, and prints a Jelly representation of a list of integers \$a(n) \forall n \in [0,b]\$.

Try it online! (This is not an efficient method so it does get slow fairly quickly.)

How?

µ²Ṁ‘rN$AÞḟạƲ¥Ƈṛ/Ḣṭø¡ - Main Link: non-negative integer, B
                   ø  - niladic chain, initialising Found to zero
µ                   ¡ - repeat the previous monadic chain {B} times:
 ²                    -   square {Found} (vectorises)
  Ṁ                   -   maximum value
   ‘                  -   increment (just to cater for the initial zero)
      $               -   last two links as a monad - f(V = max(Found²)+1)
     N                -     negate {V} -> -V
    r                 -     {V} inclusive range {-V} -> [V,V-1,...,-V]
       AÞ             -   sort by absolute value -> [0,1,-1,...,V,-V]
         ḟ            -   filter discard {Found} -> Candidates
               ṛ/     -   rightmost of {Found} -> a(n-1)
              Ƈ       -   keep those {Candidates} for which:
             ¥        -     last two links as a dyad - f(Candidate, a(n-1))
          ạ           -       {Candidate} absolute difference {a(n-1)}
           Ʋ         -       is a perfect square?
                 Ḣ    -   head -> a(n)
                  ṭ   -   tack {a(n)} to {Found} -> Found + [a(n)]

Charcoal, 74 bytes

≔⁰θ⊞υθFN«≔⁰η≔⟦⟧ζW∨¬ζ›×⌊ζ⌈ζ⁰«≦⊕η≔E²⁺θ××ηη⊖⊗λεF№υλ⊞ζλ»≔⌊↔ζθF¬№ζθ≦±θ⊞υθ»Iυ

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

≔⁰θ⊞υθ

Start with a(0)=0 as the last value and also the list of values so far.

FN«

Generate b additional values.

≔⁰η

Start with a candidate square root of 0.

≔⟦⟧ζ

Start with no candidate successors.

W∨¬ζ›×⌊ζ⌈ζ⁰«

Repeat until both positive and negative candidates have been found.

≦⊕η

Try the next square root.

≔E²⁺θ××ηη⊖⊗λε

Get the next potential candidates.

F№υλ⊞ζλ

Add them if they're not already in the a list.

»≔⌊↔ζθ

Assume the next value is the absolute minimum candidate.

F¬№ζθ≦±θ

If this value isn't a candidate then negate it.

⊞υθ

Add the value to the a list.

»Iυ

Output the a list.