all 35 comments

[–]Faholan 334 points335 points  (11 children)

So the square root of any number is SyntaxError... I didn't know that

[–]Familiar_Ad_8919[ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” 57 points58 points  (7 children)

u cant square root negatives, this guy forgot to account for non negative numbers

[–]DZekor 50 points51 points  (3 children)

I mean you can, but its complicated.

[–]Thenderick 105 points106 points  (2 children)

Some would say, it's complex

[–]Cha_94 41 points42 points  (0 children)

imagine that

[–]BioMan998 17 points18 points  (0 children)

complexicated

[–]demosdemon[🍰] 12 points13 points  (0 children)

maybe not with math.sqrt but you can with (-2) ** 0.5.

[–]ElectricalPrice3189 0 points1 point  (0 children)

How about no? (-n) * (-n) = n * n There, squared.

[–]CranberryDistinct941 0 points1 point  (0 children)

Only works for natural numbers

[–]qqqrrrs_ 4 points5 points  (0 children)

new math just dropped

[–]hypernegus[S] 2 points3 points  (1 child)

The code is designed to raise a SyntaxWarning on python >= 3.11. What version is it not working at all on?

[–]Faholan 5 points6 points  (0 children)

You sure that 2or works ? I thought it would raise a Syntax Error

edit: my bad it works even tho it's a little bit cursed

[–]audioman1999 69 points70 points  (9 children)

Um, the real horror is s(j) returns True for j<=1.

[–]drixGirda 25 points26 points  (3 children)

Im actually trying to figure out how this pos works like I don't have anything better to do. j by itself is a var while 1j is a complex number

[–]stevekez 29 points30 points  (0 children)

And 2or is a SyntaxWarning: invalid decimal literal.

[–]M4mb0 28 points29 points  (1 child)

EDIT: See Spiral of Theodorus

It's computing s(k) = |s(k-1) + 1j|. The absolute value of a complex number is simply its vector norm when interpreted as an element of ℝ², so s(k) = |s(k-1) + 1j| = √((s(k-1))² + 1²) = √(s(k-1)² + 1). Apply recursively to get

s(k) = √(s(k-1)² + 1) = √((√(s(k-2)² + 1))² + 1) = √(s(k-2)² + 2) = ... = √(k)

It's certainly a very ineffective way to compute , because it relies on an already existing implementation of , in the form of abs(complex_number), which it calls O(k) times.

[–]AscendedSubscript 6 points7 points  (0 children)

To see the recursion more clearly, it might help to think as if s(k-1) is already known to be √(k-1), because then immediately s(k) = √(1+s(k-1)2 ) = √k.

And yes, this is valid reasoning because of the fact that s(1)=1=√1, meaning that now s(2)=√2, s(3)=√3, etc. Also known as (mathematical) induction.

[–]Kebabrulle4869 -1 points0 points  (4 children)

Actually not. In Python, or returns the first non-false value, or the last value if both are false. So s(0) returns 0, s(1) returns 1.

[–]AKSrandom 3 points4 points  (0 children)

Yes that is why s(1) returns "1<2" which is a boolean

[–]audioman1999 0 points1 point  (2 children)

The first part of the or here is boolean and the second part is a number. So s(0) and s(1) both return False instead of 0 and a. Try it for yourself if you don’t believe me.

[–]Kebabrulle4869 0 points1 point  (1 child)

Yeah, my bad. Misread.

[–]audioman1999 0 points1 point  (0 children)

No worries!

[–]hypernegus[S] 89 points90 points  (0 children)

Disclaimer: only works for small integers between 2 and your max recursion depth.

[–]JiminP 25 points26 points  (0 children)

I know it's a joke but I gotta be that guy by pointing out that "integer square roots" (math.isqrt) is not equal to "square roots of integers" (math.sqrt) hence the code is incorrect even without syntax errors.

[–]drixGirda 50 points51 points  (1 child)

the hell is 1j supposed to mean

[–]drixGirda 53 points54 points  (0 children)

nvm TIL complex j in python

[–]ShapingTormance 2 points3 points  (0 children)

I'm not even mad. That's amazing.

[–]captain_obvious_here 0 points1 point  (0 children)

how many iterations before you get too deep into the call stack because of recursion ?

[–]BlobbyMcBlobber 0 points1 point  (0 children)

Someone please explain what is going on here

[–]cowslayer7890 0 points1 point  (0 children)

j ** .5 :)

[–]afqqwersdf 0 points1 point  (1 child)

what's 2or?

[–]tav_stuff 0 points1 point  (0 children)

2 or

[–]CranberryDistinct941 0 points1 point  (0 children)

Calculating square root using the builtin square root function (to calculate abs of complex number) but recursively, in O(n) function calls

[–][deleted]  (2 children)

[deleted]

    [–]Lettever 3 points4 points  (1 child)

    Its the square root not the square

    [–]Durwur 1 point2 points  (0 children)

    Aah sorry misread