# Compare positions of integers in this sequence

A001057 is one way to represent an integer as a natural number. It lists them according to the following pattern:

0, 1, -1, 2, -2, 3, -3, 4, -4, ...


In this challenge, you'll take two distinct integers as input and return which is at a higher index in this enumeration.

Take -2 and 1 as an example: -2 comes after 1, so it would be returned.

You may also choose to instead return the smaller integer, or one of two consistent values (such as 0 or 1) to indicate which is higher or lower. Truthy/falsy values as output are not allowed.

Test cases:

 0   1       1
1   0       1
0  -1      -1
-1   0      -1
-1   1      -1
1  -1      -1
1  -2      -2
1   2       2
-1  -2      -2
-1   2       2

• Can we take input as a list of the two numbers, and then output the list, sorted according to the sequence? e.g. [-6,4] -> [4,-6] Jul 26 at 17:26
• @AaronMiller No, sorry Jul 26 at 19:03
• "Truthy/falsy values as output are not allowed" is a weird restriction for a lot of languages considering you allow 0/1 (basically for all loose type ones): in Javascript or PHP, 0/1 are falsey/truthy values, as well as all the numbers in the sequence btw.. Jul 27 at 8:16
• @Kaddath I allow any two distinct outputs, not just 0 and 1. You could choose to return true and false if you want, just not any inconsistent truthy/falsy values. Jul 27 at 13:26
• Makes sense, I think it could be clearer in the post, maybe with "Inconsistent truthy/falsy values as output are not allowed."? Jul 27 at 15:51

# Python, 20 bytes

lambda x,y:x*x>y*y-y


Try it online!

Checks whether $$\x^2>y^2-y \$$.

Outputs True/False for whether x comes after y in the listing. Note that we're not asked handle the case x==y.

To see that this works, note that each of the x*x value in the table is greater than all the y*y-y values to the left of it, but none to the right of it.

       0  1  -1  2  -2  3  -3   4  -4 ...
x*x:   0  1   1  4   4  9   9  16  16 ...
y*y-y: 0  0   2  2   6  6  12  12  20 ...


The idea is that the entries come in pairs +n, -n (except 0 once), so comparing squares works to check if one input has a bigger absolute value than the other. Then, changing $$\y^2\$$ to $$\y^2-y\$$ in the second entry, makes +n "lose" to -n, but is a small enough shift compared to the difference between consecutive squares that it doesn't mess up comparisons between different squares.

• Also 20 but weirder! : lambda x,y:x*x>y*~-y Jul 27 at 16:35

# Desmos, 21 bytes


f(x,y)=\{xx-x<yy,0\}


(Newline is required)

Edit: Just noticed that xnor's answer is very similar to mines, but I found this independently.

Function is $$\f(x,y)\$$, and it returns $$\0\$$ if $$\x\$$ has a higher index, otherwise returns $$\1\$$.

This seems to work, but I just randomly stumbled upon it, so I can't explain how it works.

Try It On Desmos!

Try It On Desmos! - Prettified

• I don't think this will work for -1, 1 (or if it does, for 1, -1) Jul 26 at 16:54

# J, 8 bytes

>&(-3^|)


Try it online!

Or verbose: (x - 3 ^ abs(x)) > (y - 3 ^ abs(y)). This maps the inputs to the following sequence to compare them in:

 0  1 _1  2  _2   3  _3   4  _4    5   _5
_1 _2 _4 _7 _11 _24 _30 _77 _85 _238 _248


If sorted input as output is allowed, then 7 byte /:*.@%: is another fun way to sort /: the input list: *. generates (length, angle) pairs of the square root %: of x (which results in complex numbers for negative values):

 0       0      0
1       1      0
_1       1 1.5708
2 1.41421      0
_2 1.41421 1.5708
3 1.73205      0
_3 1.73205 1.5708

• Very nice solution Jul 26 at 17:40

# Husk, 7 bytes

F>m€Θİ±


Try it online!

Returns 1 if the second is larger than the first, 0 otherwise

• removing the Θ will work since 0 is at the first index anyway Jul 27 at 3:52
• @Razetime That hangs forever if 0 is in the input Jul 27 at 3:54
• ah forgot we were using €. Jul 27 at 4:27

# Vyxal, 5 bytes

²?Ḣ+⇧


Try it Online!

Takes input as a list and returns [1,0] for first being bigger, [0,1] for second being bigger.

²     # [x^2, y^2]
?Ḣ   # [y]
+  # [x^2+y, y^2]


# Python 3, 29 26 bytes

Returns True if a is a at a higher index than b, False otherwise.

lambda a,b:a*a+(a<0)/2>b*b


Try it online!

• How about -a==b>0? Jul 26 at 17:22
• @pxeger That would've worked ;)
– ovs
Jul 26 at 17:22

# Jelly, 5 bytes

²Ḥ_ṠỤ


Try It Online!

-1 byte thanks to ovs
-1 byte thanks to Unrelated String

Returns [1, 2] if the first integer comes first and [2, 1] for the second integer.

²Ḥ_ṠỤ    Main Link; accepts a list
²        x ** 2
Ḥ       2 * (x ** 2)
_Ṡ    2 * (x ** 2) - sign(x)

 0 => 0
1 => 1
-1 => 3
2 => 7
-2 => 9
3 => 17
-3 => 19
...

• I don't know if this stretches the two consistent values too much, but if you replace ṢƑ with Ụ, this returns [1, 2] / [2, 1] for sorted / not sorted
– ovs
Jul 26 at 17:09
• ×4 $\to$ Ḥ: Try it online! Jul 26 at 17:11
• @ovs oh yeah those are two consistent values so i see no problem with that, thanks Jul 26 at 17:25
• @cairdcoinheringaahing doesn't work for -1,2 / 2,-1 Jul 26 at 17:29
• Ḥ LIVES Jul 27 at 1:12

# Vyxal, 7 bytes

ȧ4*?±-⇧


Try it Online!

Oh yes, jelly port. Very fun.

⟨0|1⟩ for second integer being yes, ⟨1|0⟩ for first integer being yes.

## Explained

ȧ4*?±-⇧
ȧ4*     # 4 * abs(input) [vectorised] # call this x
?±   # sign_of(input) [vectorised - <0 = -1, =0 = 0, >0 = 1] # call this y
-  # x - y [vectorised element-wise] # call this z
⇧ # indexes of z in an order such that they would arrange z to be sorted ascending


# Jelly, 4 bytes

Ḥc2Ụ


Try it online!

Alternative grade-up solution. Uses (2n)C2 = n(2n-1) as the grade key function. This function maps 0, 1, -1, 2, -2, 3, -3, ... to 0, 1, 3, 6, 10, 15, 21, ..., i.e. the triangular numbers.

This allows a shorter APL solution:

# APL(Dyalog Unicode), 5 bytes SBCS

⍋2!+⍨


Try it on APLgolf!

# Japt, 5 bytes

Outputs true for the first number being higher or false for the second.

²+V>²


Try it

# COW, 183 bytes

oomMMMmoOMMMmoOoomMMMmoOMMMmOomOoMMMmoOmoOmoOMMMMMMmoOMMMmOomOoMMMmoOmoOmoOMMMMMMmoOMMMmOoMOOmOomOomOomOomOomOoMOOMOomoOmoOMOOMoOmoOmoOMOOMoOmoOmoOMOoMMMOOOmooOOOmooOOOmooMMMmoomoOOOM


Try it online!

Returns the smaller integer. It increases and decreases both, the first to reach 0 (with precedence to the decreasing one) is the smaller.

[0]: n to be decreased   [1]: n   [2]: m to be increased   [3]: m   [4]: n to be increased   [5]: n   [6]: m to be decreased   [7]: m

i=>=>i=>=<<=>>>==>=<<=>>>==>=<    | set up memory cells as above
[                                 | loop until a cell reaches 0
<<<<<<[->>[+>>[+>>-=*]*]*]=   | [0]-- [2]++ [4]++ [6]--
]>o                               | print the redundant copy of the smaller

moo ]    mOo <    MOo -    OOO *    OOM i
MOO [    moO >    MoO +    MMM =    oom o


# Python 2, 30 bytes

lambda x,y:(x*x,x<0)>(y*y,y<0)

Attempt This Online!

This is one of those irritating ones that can't be shortened with a loop because the loop is only over 2 items.

The abs(x) -> x*x trick was taken from ovs' answer.

# APL (Dyalog Unicode), 6 bytes

A port of hyper-neutrino's Jelly answer. Outputs are swapped.

⍋×-4×|


Try it online! g gets the "higher" number according to the output.

# Wolfram Language (Mathematica), 19 bytes

Most@*SortBy[-# #&]


Try it online!

-23 bytes from @att

• 19 bytes
– att
Jul 26 at 18:53

# C (gcc), 20 bytes

f(a,b){a=a*a>b*b-b;}


Try it online!

# Scala, 37 bytes

_.maxBy(x=>if(x>0)x*2-1 else x.abs*2)


Try it online!

Very simple, simply takes a list containing the two number, maps each to its index in the sequence, and finds the max according to that.

• 33 bytes: _.maxBy(x=>if(x>0)x*2-1 else-x*2). Porting my answer gives _.maxBy(x=>2*x*x-x) for 19 bytes. Jul 27 at 7:05

# Jelly, 5 4 bytes

²+ḊỤ


Try it online!

Terrible port of xnor's Python solution, also stealing from hyper-neutrino's Jelly solution ovs's output format ([1, 2] if y is greater, [2, 1] if x is greater). Takes input as a list [x, y].

²       [x^2, y^2]
+      plus
Ḋ     [y]:
²+Ḋ     [x^2 + y, y^2].


# Charcoal, 10 bytes

Ｉ⌊Φθ⁼↔ι⌈↔θ


Try it online! Link is to verbose version of code. Takes input as a list of two (actually arbitrary many) integers (which need not be distinct). Explanation: Outputs the element with the greatest absolute value, or the less of two elements tied for absolute value. Explanation:

   θ        Input list
Φ         Filter where
ι     Current element
↔      Absolute value
⁼       Equals
θ  Input list
↔   Vectorised absolute value
⌈    Take the maximum
⌊          Take the minimum
Ｉ           Cast to string
Implicitly print


Actually a port of @xnor's Python answer would probably be only 9 bytes but that would be boring.

# K (oK), 14 bytes

f:{x*x>-y+y*y}


Try it online!

• If you intended to port a*a>b*b-b, your current code is totally wrong since it is x*(x>-(y+y*y)) now. The easiest way to fix it is to parenthesize like {(x*x)>(y*y)-y}. Btw, you can exclude f: from byte count. Aug 4 at 10:12