Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), (削除) 117 ... 10199 (削除ここまで) 9998 bytes

f=n=>(G=x=>q?G(q,q=x%q):x,d=1,h=(n,x)=>++d>n?~x:1/(g=_=>q=n%d?0:g(n/=d)-1)(G=x=>q?G(q,q=x%q):x)|h(n,G(x)))(++n)?f(n):n

Try it online! Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) // (NB: actually defined in a dummy argument
 : x, // passed to g in the last golfed version)
 d = 1, // initialize the divisor d
 h = ( // h is a recursive function taking:
 n, // n = updated value
 x // x = GCD of the exponents (but negative)
 ) => //
 ++d > n ? // increment d; if it's greater than n:
 ~x // stop and yield -x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % d ? // the opposite of the number of times n can be
 0 // divided by d
 : //
 g(n /= d) // we compute 1 / g() followed by a bitwise OR
 - 1 // to make sure that q is not equal to -1
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n) ? f(n) // if the result of h is truthy, try again
 : n // otherwise, return n

JavaScript (ES6), (削除) 117 ... 101 (削除ここまで) 99 bytes

f=n=>(G=x=>q?G(q,q=x%q):x,d=1,h=(n,x)=>++d>n?~x:1/(g=_=>q=n%d?0:g(n/=d)-1)()|h(n,G(x)))(++n)?f(n):n

Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) //
 : x, //
 d = 1, // initialize the divisor d
 h = ( // h is a recursive function taking:
 n, // n = updated value
 x // x = GCD of the exponents (but negative)
 ) => //
 ++d > n ? // increment d; if it's greater than n:
 ~x // stop and yield -x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % d ? // the opposite of the number of times n can be
 0 // divided by d
 : //
 g(n /= d) // we compute 1 / g() followed by a bitwise OR
 - 1 // to make sure that q is not equal to -1
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n) ? f(n) // if the result of h is truthy, try again
 : n // otherwise, return n

JavaScript (ES6), (削除) 117 ... 99 (削除ここまで) 98 bytes

f=n=>(d=1,h=(n,x)=>++d>n?~x:1/(g=_=>q=n%d?0:g(n/=d)-1)(G=x=>q?G(q,q=x%q):x)|h(n,G(x)))(++n)?f(n):n

Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) // (NB: actually defined in a dummy argument
 : x, // passed to g in the last golfed version)
 d = 1, // initialize the divisor d
 h = ( // h is a recursive function taking:
 n, // n = updated value
 x // x = GCD of the exponents (but negative)
 ) => //
 ++d > n ? // increment d; if it's greater than n:
 ~x // stop and yield -x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % d ? // the opposite of the number of times n can be
 0 // divided by d
 : //
 g(n /= d) // we compute 1 / g() followed by a bitwise OR
 - 1 // to make sure that q is not equal to -1
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n) ? f(n) // if the result of h is truthy, try again
 : n // otherwise, return n
saved 2 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), (削除) 117 ... 102101 (削除ここまで) 10199 bytes

f=n=>(G=x=>q?G(q,q=x%q):x,d=1,h=(n,k,x)=>k>n=>++d>n?~x:1/(g=_=>q=n%kg=_=>q=n%d?0:g(n/=k=d)-1)()|h(n,k+1,G(x)))(++n,2)?f(n):n

Try it online! Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) //
 : x, //
 hd = ( 1, // h is ainitialize recursivethe functiondivisor taking:d
 h n,= ( // h is na =recursive updatedfunction valuetaking:
 kn, // kn = current divisor (greater thanupdated 1)value
 x // x = GCD of the exponents (but negative)
 ) => //
 k++d > n ? // ifincrement kd; isif it's greater than n:
 ~x // stop and yield -x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % kd ? // the opposite of the number of times n can be
 0 // divided by kd
 : //
 g(n /= kd) // we compute 1 / g() followed by a bitwise OR
 - 1 // to make sure that q is not equal to -1
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 k + 1, // increment k
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n, 2) ? f(n) // if the result of h is truthy, try again
 : n : n // otherwise, return n

JavaScript (ES6), (削除) 117 ... 102 (削除ここまで) 101 bytes

f=n=>(G=x=>q?G(q,q=x%q):x,h=(n,k,x)=>k>n?~x:1/(g=_=>q=n%k?0:g(n/=k)-1)()|h(n,k+1,G(x)))(++n,2)?f(n):n

Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) //
 : x, //
 h = (  // h is a recursive function taking:
 n, // n = updated value
 k, // k = current divisor (greater than 1)
 x // x = GCD of the exponents (but negative)
 ) => //
 k > n ? // if k is greater than n:
 ~x // stop and yield -x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % k ? // the opposite of the number of times n can be
 0 // divided by k
 : //
 g(n /= k) // we compute 1 / g() followed by a bitwise OR
 - 1 // to make sure that q is not equal to -1
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 k + 1, // increment k
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n, 2) ? f(n) // if the result of h is truthy, try again
 : n // otherwise, return n

JavaScript (ES6), (削除) 117 ... 101 (削除ここまで) 99 bytes

f=n=>(G=x=>q?G(q,q=x%q):x,d=1,h=(n,x)=>++d>n?~x:1/(g=_=>q=n%d?0:g(n/=d)-1)()|h(n,G(x)))(++n)?f(n):n

Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) //
 : x, //
 d = 1, // initialize the divisor d
 h = ( // h is a recursive function taking:
 n, // n = updated value
 x // x = GCD of the exponents (but negative)
 ) => //
 ++d > n ? // increment d; if it's greater than n:
 ~x // stop and yield -x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % d ? // the opposite of the number of times n can be
 0 // divided by d
 : //
 g(n /= d) // we compute 1 / g() followed by a bitwise OR
 - 1 // to make sure that q is not equal to -1
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n) ? f(n) // if the result of h is truthy, try again
 : n // otherwise, return n
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670

JavaScript (ES6), (削除) 117 ... 108102 (削除ここまで) 102101 bytes

f=n=>(G=x=>q?G(q,q=x%q):x,h=(n,k,x)=>k>n?x-1~x:1/(g=_=>q=n%k?0:1+gg(n/=k)-1)()|h(n,k+1,G(x)))(++n,2)?f(n):n

Try it online! Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) //
 : x, //
 h = ( // h is a recursive function taking:
 n, // n = updated value
 k, // k = current divisor (greater than 1)
 x // x = GCD of the exponents (but negative)
 ) => //
 k > n ? // if k is greater than n:
 x~x - 1 // stop and yield -x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % k ? // the opposite of the number of times n can be divided by k
 0 // divided by k
 : // g(n /= k) // we compute 1 / g() followed by a bitwise OR
 1- +1 // to make sure that q is not equal to -1
 g(n /= k) //
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 k + 1, // increment k
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n, 2) ? f(n) // if the result of h is truthy, try again
 : n // otherwise, return n

JavaScript (ES6), (削除) 117 ... 108 (削除ここまで) 102 bytes

f=n=>(G=x=>q?G(q,q=x%q):x,h=(n,k,x)=>k>n?x-1:1/(g=_=>q=n%k?0:1+g(n/=k))()|h(n,k+1,G(x)))(++n,2)?f(n):n

Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) //
 : x, //
 h = ( // h is a recursive function taking:
 n, // n = updated value
 k, // k = current divisor (greater than 1)
 x // x = GCD of the exponents
 ) => //
 k > n ? // if k is greater than n:
 x - 1 // stop and yield x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % k ? // the number of times n can be divided by k
 0 //
 : // we compute 1 / g() followed by a bitwise OR
 1 + // to make sure that q is not equal to 1
 g(n /= k) //
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 k + 1, // increment k
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n, 2) ? f(n) // if the result of h is truthy, try again
 : n // otherwise, return n

JavaScript (ES6), (削除) 117 ... 102 (削除ここまで) 101 bytes

f=n=>(G=x=>q?G(q,q=x%q):x,h=(n,k,x)=>k>n?~x:1/(g=_=>q=n%k?0:g(n/=k)-1)()|h(n,k+1,G(x)))(++n,2)?f(n):n

Try it online!

f = n => ( // f is a recursive function taking n
 G = x => // G is a helper function computing GCD(x, q)
 q ? G(q, q = x % q) //
 : x, //
 h = ( // h is a recursive function taking:
 n, // n = updated value
 k, // k = current divisor (greater than 1)
 x // x = GCD of the exponents (but negative)
 ) => //
 k > n ? // if k is greater than n:
 ~x // stop and yield -x - 1
 : // else:
 1 / (g = _ => // g is yet another recursive function
 q = // which computes q:
 n % k ? // the opposite of the number of times n can be
 0 // divided by k
 : // g(n /= k) // we compute 1 / g() followed by a bitwise OR
 - 1 // to make sure that q is not equal to -1
 )() | //
 h( // recursive call to h:
 n, // updated value of n
 k + 1, // increment k
 G(x) // x = GCD(x, q)
 ) // end of recursive call
)(++n, 2) ? f(n) // if the result of h is truthy, try again
 : n // otherwise, return n
saved 6 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 4 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
added a commented version
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 1 byte
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
saved 3 bytes
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading
Source Link
Arnauld
  • 205.5k
  • 21
  • 187
  • 670
Loading

AltStyle によって変換されたページ (->オリジナル) /