def test_isqrt(): from math import sqrt as _sqrt limit = 17984395633462800708566937239551 assert int(_sqrt(limit)) == integer_nthroot(limit, 2)[0] assert int(_sqrt(limit + 1)) != integer_nthroot(limit + 1, 2)[0] assert isqrt(limit + 1) == integer_nthroot(limit + 1, 2)[0] assert isqrt(limit + 1 + S.Half) == integer_nthroot(limit + 1, 2)[0]
def cholesky(matlist, K): """ Performs the cholesky decomposition of a Hermitian matrix and returns L and it's conjugate transpose. Examples ======== >>> from sympy.matrices.densesolve import cholesky >>> from sympy import QQ >>> cholesky([[QQ(25), QQ(15), QQ(-5)], [QQ(15), QQ(18), QQ(0)], [QQ(-5), QQ(0), QQ(11)]], QQ) ([[5, 0, 0], [3, 3, 0], [-1, 1, 3]], [[5, 3, -1], [0, 3, 1], [0, 0, 3]]) See Also ======== cholesky_solve """ new_matlist = copy.deepcopy(matlist) nrow = len(new_matlist) L = eye(nrow, K) for i in range(nrow): for j in range(i + 1): a = K.zero for k in range(j): a += L[i][k] * L[j][k] if i == j: L[i][j] = isqrt(new_matlist[i][j] - a) else: L[i][j] = (new_matlist[i][j] - a) / L[j][j] return L, conjugate_transpose(L, K)
def cholesky(matlist, K): """ Performs the cholesky decomposition of a Hermitian matrix and returns L and it's conjugate transpose. Examples ======== >>> from sympy.matrices.densesolve import cholesky >>> from sympy import QQ >>> cholesky([[QQ(25), QQ(15), QQ(-5)], [QQ(15), QQ(18), QQ(0)], [QQ(-5), QQ(0), QQ(11)]], QQ) ([[5, 0, 0], [3, 3, 0], [-1, 1, 3]], [[5, 3, -1], [0, 3, 1], [0, 0, 3]]) See Also ======== cholesky_solve """ new_matlist = copy.deepcopy(matlist) nrow = len(new_matlist) L = eye(nrow, K) for i in range(nrow): for j in range(i + 1): a = K.zero for k in range(j): a += L[i][k]*L[j][k] if i == j: L[i][j] = isqrt(new_matlist[i][j] - a) else: L[i][j] = (new_matlist[i][j] - a)/L[j][j] return L, conjugate_transpose(L, K)
def count_brute(n): if n > 10 ** 8: return None count = 0 for y in range(isqrt(n)+2): y2 = y*y for x in range(y+1): if y2 + y*x + x*x == n: count += 1 return count
def count_circle(n): # Circle walk x^2 + y^2 + x*y if n > (22 * 10 ** 7) ** 2: return None count = 0 # 0 <= x <= y # Largest value is at (0, y) => y^2 # Smallest value is at (y, y) => 3*y^2 minY = isqrt(n // 3) maxY = isqrt(n) x = 0 for y in reversed(range(minY, maxY+1)): test = x*x + x*y + y*y while test < n and x < y: x += 1 test = x*x + x*y + y*y if test == n: count += 1 # print("\t", test, " ", x, y) return count
def _discrete_log_shanks_steps(n, a, b, order=None): """ Baby-step giant-step algorithm for computing the discrete logarithm of ``a`` to the base ``b`` modulo ``n``. The algorithm is a time-memory trade-off of the method of exhaustive search. It uses `O(sqrt(m))` memory, where `m` is the group order. Examples ======== >>> from sympy.ntheory.residue_ntheory import _discrete_log_shanks_steps >>> _discrete_log_shanks_steps(41, 15, 7) 3 See Also ======== discrete_log References ========== .. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., & Vanstone, S. A. (1997). """ a %= n b %= n if order is None: order = n_order(b, n) m = isqrt(order) + 1 T = dict() x = 1 for i in range(m): T[x] = i x = x * b % n z = mod_inverse(b, n) z = pow(z, m, n) x = a for i in range(m): if x in T: return i * m + T[x] x = x * z % n raise ValueError("Log does not exist")
def _discrete_log_shanks_steps(n, a, b, order=None): """ Baby-step giant-step algorithm for computing the discrete logarithm of ``a`` to the base ``b`` modulo ``n``. The algorithm is a time-memory trade-off of the method of exhaustive search. It uses `O(sqrt(m))` memory, where `m` is the group order. References ========== .. [1] "Handbook of applied cryptography", Menezes, A. J., Van, O. P. C., & Vanstone, S. A. (1997). Examples ======== >>> from sympy.ntheory.residue_ntheory import _discrete_log_shanks_steps >>> _discrete_log_shanks_steps(41, 15, 7) 3 See also ======== discrete_log """ a %= n b %= n if order is None: order = n_order(b, n) m = isqrt(order) + 1 T = dict() x = 1 for i in range(m): T[x] = i x = x * b % n z = mod_inverse(b, n) z = pow(z, m, n) x = a for i in range(m): if x in T: return i * m + T[x] x = x * z % n raise ValueError("Log does not exist")