16
\$\begingroup\$

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
\$\endgroup\$
7
  • 2
    \$\begingroup\$ 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] \$\endgroup\$ Jul 26, 2021 at 17:26
  • \$\begingroup\$ @AaronMiller No, sorry \$\endgroup\$ Jul 26, 2021 at 19:03
  • 2
    \$\begingroup\$ "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.. \$\endgroup\$
    – Kaddath
    Jul 27, 2021 at 8:16
  • \$\begingroup\$ @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. \$\endgroup\$ Jul 27, 2021 at 13:26
  • 2
    \$\begingroup\$ Makes sense, I think it could be clearer in the post, maybe with "Inconsistent truthy/falsy values as output are not allowed."? \$\endgroup\$
    – Kaddath
    Jul 27, 2021 at 15:51

19 Answers 19

11
\$\begingroup\$

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.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Also 20 but weirder! : lambda x,y:x*x>y*~-y \$\endgroup\$
    – aeh5040
    Jul 27, 2021 at 16:35
6
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ Very nice solution \$\endgroup\$
    – Jonah
    Jul 26, 2021 at 17:40
5
\$\begingroup\$

Husk, 7 bytes

F>m€Θݱ

Try it online!

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

\$\endgroup\$
3
  • \$\begingroup\$ removing the Θ will work since 0 is at the first index anyway \$\endgroup\$
    – Razetime
    Jul 27, 2021 at 3:52
  • 1
    \$\begingroup\$ @Razetime That hangs forever if 0 is in the input \$\endgroup\$ Jul 27, 2021 at 3:54
  • \$\begingroup\$ ah forgot we were using . \$\endgroup\$
    – Razetime
    Jul 27, 2021 at 4:27
5
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ I don't think this will work for -1, 1 (or if it does, for 1, -1) \$\endgroup\$
    – hyper-neutrino
    Jul 26, 2021 at 16:54
4
\$\begingroup\$

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]
    ⇧ # Grade up
\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
2
  • \$\begingroup\$ How about -a==b>0? \$\endgroup\$
    – pxeger
    Jul 26, 2021 at 17:22
  • \$\begingroup\$ @pxeger That would've worked ;) \$\endgroup\$
    – ovs
    Jul 26, 2021 at 17:22
3
\$\begingroup\$

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)
     Ụ   Grade up
 0 => 0
 1 => 1
-1 => 3
 2 => 7
-2 => 9
 3 => 17
-3 => 19
...
\$\endgroup\$
6
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – ovs
    Jul 26, 2021 at 17:09
  • 1
    \$\begingroup\$ ×4 \$\to\$ : Try it online! \$\endgroup\$ Jul 26, 2021 at 17:11
  • 1
    \$\begingroup\$ @ovs oh yeah those are two consistent values so i see no problem with that, thanks \$\endgroup\$
    – hyper-neutrino
    Jul 26, 2021 at 17:25
  • 1
    \$\begingroup\$ @cairdcoinheringaahing doesn't work for -1,2 / 2,-1 \$\endgroup\$
    – hyper-neutrino
    Jul 26, 2021 at 17:29
  • 1
    \$\begingroup\$ LIVES \$\endgroup\$ Jul 27, 2021 at 1:12
2
\$\begingroup\$

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
\$\endgroup\$
0
2
\$\begingroup\$

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!

\$\endgroup\$
2
\$\begingroup\$

Japt, 5 bytes

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

²+V>²

Try it

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

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.

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

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.

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

Wolfram Language (Mathematica), 19 bytes

Most@*SortBy[-# #&]

Try it online!

-23 bytes from @att

\$\endgroup\$
1
  • \$\begingroup\$ 19 bytes \$\endgroup\$
    – att
    Jul 26, 2021 at 18:53
1
\$\begingroup\$

C (gcc), 20 bytes

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

Try it online!

Port of xnor's Python answer.

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

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Bubbler
    Jul 27, 2021 at 7:05
1
\$\begingroup\$

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].
   Ụ    Grade up.
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 10 bytes

I⌊Φθ⁼↔ι⌈↔θ

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
I           Cast to string
            Implicitly print

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

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

K (oK), 14 bytes

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Bubbler
    Aug 4, 2021 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.