Skip to main content
Commonmark migration
Source Link

#JavaScript (ES7), 55 53 bytes

JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)

Try it online!

###How?

How?

Given an array \$q=[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

q =>                            // q[] = input array
  q.map(v =>                    // for each value v in q[]:
    1 / q ?                     //   if q is numeric (2nd to 4th iteration):
      v / 2 / q                 //     yield v / 2q
    :                           //   else (1st iteration, with v = a):
      q = (                     //     compute x (as defined above) and store it in q
        (v + Math.hypot(...q))  //     we use Math.hypot(...q) to compute:
        / 2                     //       (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
      ) ** .5                   //     yield x
  )                             // end of map()

#JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)

Try it online!

###How?

Given an array \$q=[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

q =>                            // q[] = input array
  q.map(v =>                    // for each value v in q[]:
    1 / q ?                     //   if q is numeric (2nd to 4th iteration):
      v / 2 / q                 //     yield v / 2q
    :                           //   else (1st iteration, with v = a):
      q = (                     //     compute x (as defined above) and store it in q
        (v + Math.hypot(...q))  //     we use Math.hypot(...q) to compute:
        / 2                     //       (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
      ) ** .5                   //     yield x
  )                             // end of map()

JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)

Try it online!

How?

Given an array \$q=[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

q =>                            // q[] = input array
  q.map(v =>                    // for each value v in q[]:
    1 / q ?                     //   if q is numeric (2nd to 4th iteration):
      v / 2 / q                 //     yield v / 2q
    :                           //   else (1st iteration, with v = a):
      q = (                     //     compute x (as defined above) and store it in q
        (v + Math.hypot(...q))  //     we use Math.hypot(...q) to compute:
        / 2                     //       (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
      ) ** .5                   //     yield x
  )                             // end of map()
variables renamed to avoid confusion
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

a=>aq=>q.map(n=>1v=>1/aq?nv/2/aq:a=q=((n+Mathv+Math.hypot(...aq))/2)**.5)

Try it online!Try it online!

###How?

Given an array \$[a,b,c,d]\$\$q=[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

aq =>                            // a[]q[] = input array
  aq.map(nv =>                    // for each value nv in a[]q[]:
    1 / aq ?                     //   if aq is numeric (2nd to 4th iteration):
      nv / 2 / aq                 //     yield nv / 2a2q
    :                           //   else (1st iteration, with v = a):
      aq = (                     //     compute x (as defined above) and store it in aq
        (nv + Math.hypot(...aq))  //     we use Math.hypot(...aq) to compute:
        / 2                     //       (a[0]**2q[0]**2 + a[1]**2q[1]**2 + a[2]**2q[2]**2 + a[3]**2q[3]**2) ** 0.5
      ) ** .5                   //     yield x
  )                             // end of map()

#JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

a=>a.map(n=>1/a?n/2/a:a=((n+Math.hypot(...a))/2)**.5)

Try it online!

###How?

Given an array \$[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

a =>                            // a[] = input array
  a.map(n =>                    // for each value n in a[]:
    1 / a ?                     //   if a is numeric (2nd to 4th iteration):
      n / 2 / a                 //     yield n / 2a
    :                           //   else (1st iteration):
      a = (                     //     compute x (as defined above) and store it in a
        (n + Math.hypot(...a))  //     we use Math.hypot(...a) to compute:
        / 2                     //       (a[0]**2 + a[1]**2 + a[2]**2 + a[3]**2) ** 0.5
      ) ** .5                   //     yield x
  )                             // end of map()

#JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

q=>q.map(v=>1/q?v/2/q:q=((v+Math.hypot(...q))/2)**.5)

Try it online!

###How?

Given an array \$q=[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

q =>                            // q[] = input array
  q.map(v =>                    // for each value v in q[]:
    1 / q ?                     //   if q is numeric (2nd to 4th iteration):
      v / 2 / q                 //     yield v / 2q
    :                           //   else (1st iteration, with v = a):
      q = (                     //     compute x (as defined above) and store it in q
        (v + Math.hypot(...q))  //     we use Math.hypot(...q) to compute:
        / 2                     //       (q[0]**2 + q[1]**2 + q[2]**2 + q[3]**2) ** 0.5
      ) ** .5                   //     yield x
  )                             // end of map()
saved 2 bytes and added an explanation
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650

#JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

a=>a.map(n=>1/a?n/2/a:a=((n+Math.hypot(...a))/2)**.5)

Try it online!

###How?

Given an array \$[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

a =>                            // a[] = input array
  a.map(n =>                    // for each value n in a[]:
    1 / a ?                     //   if a is numeric (2nd to 4th iteration):
      n / 2 / a                 //     yield n / 2a
    :                           //   else (1st iteration):
      a = (                     //     compute x (as defined above) and store it in a
        (n + Math.hypot(...a))  //     we use Math.hypot(...a) to compute:
        / 2                     //       (a[0]**2 + a[1]**2 + a[2]**2 + a[3]**2) ** 0.5
      ) ** .5                   //     yield x
  )                             // end of map()

#JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

a=>a.map(n=>1/a?n/2/a:a=((n+Math.hypot(...a))/2)**.5)

Try it online!

###How?

Given an array \$[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

a =>                            // a[] = input array
  a.map(n =>                    // for each value n in a[]:
    1 / a ?                     //   if a is numeric (2nd to 4th iteration):
      n / 2 / a                 //     yield n / 2a
    :                           //   else:
      a = (                     //     compute x (as defined above) and store it in a
        (n + Math.hypot(...a))  //     we use Math.hypot(...a) to compute:
        / 2                     //     (a[0]**2 + a[1]**2 + a[2]**2 + a[3]**2) ** 0.5
      ) ** .5                   //
  )                             // end of map()

#JavaScript (ES7), 55 53 bytes

Based on the direct formula used by xnor.

Takes input as an array.

a=>a.map(n=>1/a?n/2/a:a=((n+Math.hypot(...a))/2)**.5)

Try it online!

###How?

Given an array \$[a,b,c,d]\$, this computes:

$$x=\sqrt{\frac{a+\sqrt{a^2+b^2+c^2+d^2}}{2}}$$

And returns:

$$\left[x,\frac{b}{2x},\frac{c}{2x},\frac{d}{2x}\right]$$

a =>                            // a[] = input array
  a.map(n =>                    // for each value n in a[]:
    1 / a ?                     //   if a is numeric (2nd to 4th iteration):
      n / 2 / a                 //     yield n / 2a
    :                           //   else (1st iteration):
      a = (                     //     compute x (as defined above) and store it in a
        (n + Math.hypot(...a))  //     we use Math.hypot(...a) to compute:
        / 2                     //       (a[0]**2 + a[1]**2 + a[2]**2 + a[3]**2) ** 0.5
      ) ** .5                   //     yield x
  )                             // end of map()
saved 2 bytes and added an explanation
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading
Source Link
Arnauld
  • 197.7k
  • 20
  • 179
  • 650
Loading