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 Revisions

4 of 7
added 229 characters in body
Nick Kennedy
  • 21.2k
  • 3
  • 18
  • 44

Jelly, 15 bytes

32:2_2:Ẹ¡:2+μƬṪ

Try it online!

An arbitrary precision Jelly answer that uses the Newton-Raphson method to find the correct answer. Uses only integer arithmetic operations so the intermediate values are all Python big ints rather than getting cast as floats which would lose precision. The integer result equates to the floor of what would be the floating point answer.

A full program that takes an integer as its argument and returns an integer.

Now correctly handles inputs of 0 and 1; previously threw an error because of division of 0 by 0 being impermissible for integers.

Explanation

 μƬ | Do the following as a monad until no new values seen, collecting up the intermediate values:
3 | - Original argument to program
 2 | - Squared
 :2 | - Integer divide by 2
 _2 | - Subtract current estimate squared
 : | - Integer divide by current estimate
 :2 | - Integer divide by 2
 + | - Add to current estimate
 Ṫ | Finally, take the tail of the list of estimates

A much shorter answer that is only accurate to float precision is:

Jelly, 4 bytes

21⁄2:@

Try it online!

Nick Kennedy
  • 21.2k
  • 3
  • 18
  • 44

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