Skip to main content
minor clarification
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>     // f is a recursive function taking the input n
n ?          // if n is not zero:
  (          //
    -n % 4   //   the sign of n % m is the sign of n in JS,
             //   so this gives a value in ]-4, 0]
    / 3      //   we turn this into a value in ]-4/3, 0]
             //   this is ≤ -1 if the 2 least significant bits
             //   of the integer part of n are set
    | 1      //   a bitwise OR with 1 gives either:
 1 or         //     -1
  for )]-4/3, -1]
           //      1 for ]-1, 0]
  *)        //
  *        //   we multiply by the result of ...
  f(n / 2)   //   ... a recursive call with n / 2
             //   once we have n < 1, the final result is not
             //   changed anymore; and once n is small enough,
             //   n / 2 will eventually be evaluated to 0
:            // else:
  1          //   stop the recursion

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>     // f is a recursive function taking the input n
n ?          // if n is not zero:
  (          //
    -n % 4   //   the sign of n % m is the sign of n in JS,
             //   so this gives a value in ]-4, 0]
    / 3      //   we turn this into a value in ]-4/3, 0]
             //   this is ≤ -1 if the 2 least significant bits
             //   of the integer part of n are set
    | 1      //   a bitwise OR with 1 gives either 1 or -1
   )          //
  *          //   we multiply by the result of ...
  f(n / 2)   //   ... a recursive call with n / 2
             //   once we have n < 1, the final result is not
             //   changed anymore; and once n is small enough,
             //   n / 2 will eventually be evaluated to 0
:            // else:
  1          //   stop the recursion

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>   // f is a recursive function taking the input n
n ?        // if n is not zero:
  (        //
    -n % 4 //   the sign of n % m is the sign of n in JS,
           //   so this gives a value in ]-4, 0]
    / 3    //   we turn this into a value in ]-4/3, 0]
           //   this is ≤ -1 if the 2 least significant bits
           //   of the integer part of n are set
    | 1    //   a bitwise OR with 1 gives either:
           //     -1 for ]-4/3, -1]
           //      1 for ]-1, 0]
  )        //
  *        //   we multiply by the result of ...
  f(n / 2) //   ... a recursive call with n / 2
           //   once we have n < 1, the final result is not
           //   changed anymore; and once n is small enough,
           //   n / 2 will eventually be evaluated to 0
:          // else:
  1        //   stop the recursion
minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>     // f is a recursive function taking the input n
n ?          // if n is not zero:
  (          //
    -n % 4   //   the sign of n % m is the sign of n in JS,
             //   so this gives 0,a -1,value in ]-24, -30]
      / 3      //   orwe turn ifthis ninto isa notvalue anin integer]-4/3, anymore0]
    / 3        //   dividing bythis 3is gives -1 if nthe %2 4least ==significant 3,bits
             //   orof athe valueinteger inpart ]-1,of 0]n otherwiseare set
    | 1      //   a bitwise OR with 1 gives either 1 or -1
  )          //
  *          //   we multiply by the result of ...
  f(n / 2)   //   ... a recursive call with n / 2
             //   once we have n < 1, the final result is not
             //   changed anymore; and once n is small enough,
             //   n / 2 will eventually be evaluated to 0
:            // else:
  1          //   stop the recursion

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>     // f is a recursive function taking the input n
n ?          // if n is not zero:
  (          //
    -n % 4   //   the sign of n % m is the sign of n in JS,
             //   so this gives 0, -1, -2, -3
             //   or  if n is not an integer anymore
    / 3      //   dividing by 3 gives -1 if n % 4 == 3,
             //   or a value in ]-1, 0] otherwise
    | 1      //   a bitwise OR with 1 gives either 1 or -1
  )          //
  *          //   we multiply by the result of ...
  f(n / 2)   //   ... a recursive call with n / 2
             //   once we have n < 1, the final result is not
             //   changed anymore; and once n is small enough,
             //   n / 2 will eventually be evaluated to 0
:            // else:
  1          //   stop the recursion

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>     // f is a recursive function taking the input n
n ?          // if n is not zero:
  (          //
    -n % 4   //   the sign of n % m is the sign of n in JS,
             //   so this gives a value in ]-4, 0]
    / 3      //   we turn this into a value in ]-4/3, 0]
             //   this is  -1 if the 2 least significant bits
             //   of the integer part of n are set
    | 1      //   a bitwise OR with 1 gives either 1 or -1
  )          //
  *          //   we multiply by the result of ...
  f(n / 2)   //   ... a recursive call with n / 2
             //   once we have n < 1, the final result is not
             //   changed anymore; and once n is small enough,
             //   n / 2 will eventually be evaluated to 0
:            // else:
  1          //   stop the recursion
minor update
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>     // f is a recursive function taking the input n
n ?          // if n is not zero:
  (          //
    -n % 4   //   the sign of n % m is the sign of n in JS,
             //   so this gives 0, -1, -2, -3
             //   or -ε if n is not an integer anymore
    / 3      //   dividing by 3 gives -1 if n % 4 == 3,
             //   or a value in ]-1, 0] otherwise
    | 1      //   a bitwise OR with 1 gives either 1 or -1
  )          //
  *          //   we multiply by the result of a recursive call...
  f(n / 2)   //   ... a recursive call with n / 2
             //   once we have n < 1, the final result is not
             //   changed anymore; and once n is small enough,
             //   n / 2 will eventually be evaluated to 0
:            // else:
  1          //   stop the recursion

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>     // f is a recursive function taking the input n
n ?          // if n is not zero:
  (          //
    -n % 4   //   the sign of n % m is the sign of n in JS,
             //   so this gives 0, -1, -2, -3
             //   or -ε if n is not an integer anymore
    / 3      //   dividing by 3 gives -1 if n % 4 == 3,
             //   or a value in ]-1, 0] otherwise
    | 1      //   a bitwise OR with 1 gives either 1 or -1
  )          //
  *          //   multiply by the result of a recursive call
  f(n / 2)   //   with n / 2
             //   once we have n < 1, the final result is not
             //   changed anymore; and once n is small enough,
             //   n / 2 will eventually be evaluated to 0
:            // else:
  1          //   stop the recursion

JavaScript (ES6), 26 bytes

Returns the \$n\$-th term of the sequence. Relies on arithmetic underflow to stop the recursion.

f=n=>n?(-n%4/3|1)*f(n/2):1

Try it online!

Commented

f = n =>     // f is a recursive function taking the input n
n ?          // if n is not zero:
  (          //
    -n % 4   //   the sign of n % m is the sign of n in JS,
             //   so this gives 0, -1, -2, -3
             //   or -ε if n is not an integer anymore
    / 3      //   dividing by 3 gives -1 if n % 4 == 3,
             //   or a value in ]-1, 0] otherwise
    | 1      //   a bitwise OR with 1 gives either 1 or -1
  )          //
  *          //   we multiply by the result of ...
  f(n / 2)   //   ... a recursive call with n / 2
             //   once we have n < 1, the final result is not
             //   changed anymore; and once n is small enough,
             //   n / 2 will eventually be evaluated to 0
:            // else:
  1          //   stop the recursion
added a commented version
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
saved 1 byte
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.6k
  • 20
  • 179
  • 650
Loading