How to use 'python square root' in Python

Every line of 'python square root' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
632def square_root(self):
633 """
634 Return square root, which is a complex number.
635
636 EXAMPLES:
637 sage: i = ComplexField(100).0
638 sage: (-i).sqrt()
639 0.70710678118654752440084436210459 - 0.70710678118654752440084436210459*I
640 """
641 return self.sqrt()
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

66def square_root_mod_p(a, p):
67 """
68 Iterator yielding values v s.t. v * v % p == a
69 There will be 0 or 2 answers.
70 """
71 # see http://course1.winona.edu/eerrthum/13Spring/SquareRoots.pdf
72 a %= p
73 if p == 2:
74 yield a & 1
75 return
76 if p == 4:
77 a &= 3
78 if a == 0:
79 yield 0
80 if a == 1:
81 yield 1
82 yield 3
83 return
84 if p & 3 == 3:
85 s1 = pow(a, (p + 1) >> 2, p)
86 if s1 * s1 % p == a:
87 yield s1
88 yield p - s1
89 return
90 if p & 7 == 5:
91 k = (p - 5) >> 3
92 v1 = pow(a, (p-1) >> 2, p)
93 v2 = pow(a, k+1, p)
94 if v1 == 1:
95 yield v2
96 yield p - v2
97 elif v1 == p-1:
98 r = (v2 * pow(2, 2*k+1, p)) % p
99 yield r
100 yield p - r
101 return
102 else:
103 raise ValueError("not implemented for prime %d" % p)

Related snippets