diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0ebd6dfa0..33069a807 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v3.4.0 + rev: v4.1.0 hooks: - id: check-executables-have-shebangs - id: check-yaml @@ -14,26 +14,26 @@ repos: - id: requirements-txt-fixer - repo: https://github.com/psf/black - rev: 21.4b0 + rev: 22.1.0 hooks: - id: black - repo: https://github.com/PyCQA/isort - rev: 5.8.0 + rev: 5.10.1 hooks: - id: isort args: - --profile=black - repo: https://github.com/asottile/pyupgrade - rev: v2.29.0 + rev: v2.31.0 hooks: - id: pyupgrade args: - --py39-plus - repo: https://gitlab.com/pycqa/flake8 - rev: 3.9.1 + rev: 3.9.2 hooks: - id: flake8 args: @@ -42,7 +42,7 @@ repos: - --max-line-length=88 - repo: https://github.com/pre-commit/mirrors-mypy - rev: v0.910 + rev: v0.931 hooks: - id: mypy args: @@ -51,11 +51,11 @@ repos: - --non-interactive - repo: https://github.com/codespell-project/codespell - rev: v2.0.0 + rev: v2.1.0 hooks: - id: codespell args: - - --ignore-words-list=ans,crate,fo,followings,hist,iff,mater,secant,som,tim + - --ignore-words-list=ans,crate,fo,followings,hist,iff,mater,secant,som,sur,tim - --skip="./.*,./strings/dictionary.txt,./strings/words.txt,./project_euler/problem_022/p022_names.txt" exclude: | (?x)^( diff --git a/DIRECTORY.md b/DIRECTORY.md index 550920c0f..b5ddb9fcb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -856,8 +856,6 @@ * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_135/sol1.py) * Problem 144 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_144/sol1.py) - * Problem 145 - * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_145/sol1.py) * Problem 173 * [Sol1](https://github.com/TheAlgorithms/Python/blob/master/project_euler/problem_173/sol1.py) * Problem 174 diff --git a/arithmetic_analysis/bisection.py b/arithmetic_analysis/bisection.py index 0ef691678..1feb4a8cf 100644 --- a/arithmetic_analysis/bisection.py +++ b/arithmetic_analysis/bisection.py @@ -32,7 +32,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float: raise ValueError("could not find root in given interval.") else: mid: float = start + (end - start) / 2.0 - while abs(start - mid) > 10 ** -7: # until precisely equals to 10^-7 + while abs(start - mid) > 10**-7: # until precisely equals to 10^-7 if function(mid) == 0: return mid elif function(mid) * function(start) < 0: @@ -44,7 +44,7 @@ def bisection(function: Callable[[float], float], a: float, b: float) -> float: def f(x: float) -> float: - return x ** 3 - 2 * x - 5 + return x**3 - 2 * x - 5 if __name__ == "__main__": diff --git a/arithmetic_analysis/in_static_equilibrium.py b/arithmetic_analysis/in_static_equilibrium.py index 6e8d1d043..6fe84b454 100644 --- a/arithmetic_analysis/in_static_equilibrium.py +++ b/arithmetic_analysis/in_static_equilibrium.py @@ -23,7 +23,7 @@ def polar_force( def in_static_equilibrium( - forces: ndarray, location: ndarray, eps: float = 10 ** -1 + forces: ndarray, location: ndarray, eps: float = 10**-1 ) -> bool: """ Check if a system is in equilibrium. diff --git a/arithmetic_analysis/intersection.py b/arithmetic_analysis/intersection.py index 204dd5d8a..9d4651144 100644 --- a/arithmetic_analysis/intersection.py +++ b/arithmetic_analysis/intersection.py @@ -35,7 +35,7 @@ def intersection(function: Callable[[float], float], x0: float, x1: float) -> fl x_n2: float = x_n1 - ( function(x_n1) / ((function(x_n1) - function(x_n)) / (x_n1 - x_n)) ) - if abs(x_n2 - x_n1) < 10 ** -5: + if abs(x_n2 - x_n1) < 10**-5: return x_n2 x_n = x_n1 x_n1 = x_n2 diff --git a/arithmetic_analysis/newton_method.py b/arithmetic_analysis/newton_method.py index a9a943726..f0cf4eaa6 100644 --- a/arithmetic_analysis/newton_method.py +++ b/arithmetic_analysis/newton_method.py @@ -37,17 +37,17 @@ def newton( next_guess = prev_guess - function(prev_guess) / derivative(prev_guess) except ZeroDivisionError: raise ZeroDivisionError("Could not find root") from None - if abs(prev_guess - next_guess) < 10 ** -5: + if abs(prev_guess - next_guess) < 10**-5: return next_guess prev_guess = next_guess def f(x: float) -> float: - return (x ** 3) - (2 * x) - 5 + return (x**3) - (2 * x) - 5 def f1(x: float) -> float: - return 3 * (x ** 2) - 2 + return 3 * (x**2) - 2 if __name__ == "__main__": diff --git a/arithmetic_analysis/newton_raphson.py b/arithmetic_analysis/newton_raphson.py index 1a8205386..86ff9d350 100644 --- a/arithmetic_analysis/newton_raphson.py +++ b/arithmetic_analysis/newton_raphson.py @@ -11,7 +11,7 @@ from sympy import diff def newton_raphson( - func: str, a: float | Decimal, precision: float = 10 ** -10 + func: str, a: float | Decimal, precision: float = 10**-10 ) -> float: """Finds root from the point 'a' onwards by Newton-Raphson method >>> newton_raphson("sin(x)", 2) diff --git a/ciphers/deterministic_miller_rabin.py b/ciphers/deterministic_miller_rabin.py index d7fcb67e9..2191caf63 100644 --- a/ciphers/deterministic_miller_rabin.py +++ b/ciphers/deterministic_miller_rabin.py @@ -73,7 +73,7 @@ def miller_rabin(n: int, allow_probable: bool = False) -> bool: for prime in plist: pr = False for r in range(s): - m = pow(prime, d * 2 ** r, n) + m = pow(prime, d * 2**r, n) # see article for analysis explanation for m if (r == 0 and m == 1) or ((m + 1) % n == 0): pr = True diff --git a/ciphers/rabin_miller.py b/ciphers/rabin_miller.py index 65c162984..c42ad2f59 100644 --- a/ciphers/rabin_miller.py +++ b/ciphers/rabin_miller.py @@ -21,7 +21,7 @@ def rabinMiller(num: int) -> bool: return False else: i = i + 1 - v = (v ** 2) % num + v = (v**2) % num return True diff --git a/ciphers/rsa_cipher.py b/ciphers/rsa_cipher.py index b1e8a73f3..5bb9f9916 100644 --- a/ciphers/rsa_cipher.py +++ b/ciphers/rsa_cipher.py @@ -29,8 +29,8 @@ def get_text_from_blocks( block_message: list[str] = [] for i in range(block_size - 1, -1, -1): if len(message) + i < message_length: - ascii_number = block_int // (BYTE_SIZE ** i) - block_int = block_int % (BYTE_SIZE ** i) + ascii_number = block_int // (BYTE_SIZE**i) + block_int = block_int % (BYTE_SIZE**i) block_message.insert(0, chr(ascii_number)) message.extend(block_message) return "".join(message) diff --git a/ciphers/rsa_factorization.py b/ciphers/rsa_factorization.py index 6df32b6cc..de4df2777 100644 --- a/ciphers/rsa_factorization.py +++ b/ciphers/rsa_factorization.py @@ -40,7 +40,7 @@ def rsafactor(d: int, e: int, N: int) -> list[int]: while True: if t % 2 == 0: t = t // 2 - x = (g ** t) % N + x = (g**t) % N y = math.gcd(x - 1, N) if x > 1 and y > 1: p = y diff --git a/computer_vision/harris_corner.py b/computer_vision/harris_corner.py index 02deb5408..886ff52ea 100644 --- a/computer_vision/harris_corner.py +++ b/computer_vision/harris_corner.py @@ -39,8 +39,8 @@ class Harris_Corner: color_img = img.copy() color_img = cv2.cvtColor(color_img, cv2.COLOR_GRAY2RGB) dy, dx = np.gradient(img) - ixx = dx ** 2 - iyy = dy ** 2 + ixx = dx**2 + iyy = dy**2 ixy = dx * dy k = 0.04 offset = self.window_size // 2 @@ -56,9 +56,9 @@ class Harris_Corner: y - offset : y + offset + 1, x - offset : x + offset + 1 ].sum() - det = (wxx * wyy) - (wxy ** 2) + det = (wxx * wyy) - (wxy**2) trace = wxx + wyy - r = det - k * (trace ** 2) + r = det - k * (trace**2) # Can change the value if r > 0.5: corner_list.append([x, y, r]) diff --git a/digital_image_processing/filters/gabor_filter.py b/digital_image_processing/filters/gabor_filter.py index 90aa049c2..8f9212a35 100644 --- a/digital_image_processing/filters/gabor_filter.py +++ b/digital_image_processing/filters/gabor_filter.py @@ -49,7 +49,7 @@ def gabor_filter_kernel( # fill kernel gabor[y, x] = np.exp( - -(_x ** 2 + gamma ** 2 * _y ** 2) / (2 * sigma ** 2) + -(_x**2 + gamma**2 * _y**2) / (2 * sigma**2) ) * np.cos(2 * np.pi * _x / lambd + psi) return gabor diff --git a/digital_image_processing/index_calculation.py b/digital_image_processing/index_calculation.py index 4350b8603..033334af8 100644 --- a/digital_image_processing/index_calculation.py +++ b/digital_image_processing/index_calculation.py @@ -203,7 +203,7 @@ class IndexCalculation: https://www.indexdatabase.de/db/i-single.php?id=391 :return: index """ - return self.nir * (self.red / (self.green ** 2)) + return self.nir * (self.red / (self.green**2)) def GLI(self): """ @@ -295,7 +295,7 @@ class IndexCalculation: """ return a * ( (self.nir - a * self.red - b) - / (a * self.nir + self.red - a * b + X * (1 + a ** 2)) + / (a * self.nir + self.red - a * b + X * (1 + a**2)) ) def BWDRVI(self): @@ -363,7 +363,7 @@ class IndexCalculation: https://www.indexdatabase.de/db/i-single.php?id=25 :return: index """ - n = (2 * (self.nir ** 2 - self.red ** 2) + 1.5 * self.nir + 0.5 * self.red) / ( + n = (2 * (self.nir**2 - self.red**2) + 1.5 * self.nir + 0.5 * self.red) / ( self.nir + self.red + 0.5 ) return n * (1 - 0.25 * n) - (self.red - 0.125) / (1 - self.red) diff --git a/electronics/carrier_concentration.py b/electronics/carrier_concentration.py index 87bcad8df..03482f1e3 100644 --- a/electronics/carrier_concentration.py +++ b/electronics/carrier_concentration.py @@ -53,12 +53,12 @@ def carrier_concentration( elif electron_conc == 0: return ( "electron_conc", - intrinsic_conc ** 2 / hole_conc, + intrinsic_conc**2 / hole_conc, ) elif hole_conc == 0: return ( "hole_conc", - intrinsic_conc ** 2 / electron_conc, + intrinsic_conc**2 / electron_conc, ) elif intrinsic_conc == 0: return ( diff --git a/electronics/coulombs_law.py b/electronics/coulombs_law.py index e4c8391c9..e41c0410c 100644 --- a/electronics/coulombs_law.py +++ b/electronics/coulombs_law.py @@ -66,13 +66,13 @@ def couloumbs_law( if distance < 0: raise ValueError("Distance cannot be negative") if force == 0: - force = COULOMBS_CONSTANT * charge_product / (distance ** 2) + force = COULOMBS_CONSTANT * charge_product / (distance**2) return {"force": force} elif charge1 == 0: - charge1 = abs(force) * (distance ** 2) / (COULOMBS_CONSTANT * charge2) + charge1 = abs(force) * (distance**2) / (COULOMBS_CONSTANT * charge2) return {"charge1": charge1} elif charge2 == 0: - charge2 = abs(force) * (distance ** 2) / (COULOMBS_CONSTANT * charge1) + charge2 = abs(force) * (distance**2) / (COULOMBS_CONSTANT * charge1) return {"charge2": charge2} elif distance == 0: distance = (COULOMBS_CONSTANT * charge_product / abs(force)) ** 0.5 diff --git a/graphics/bezier_curve.py b/graphics/bezier_curve.py index 2bb764fdc..7c22329ad 100644 --- a/graphics/bezier_curve.py +++ b/graphics/bezier_curve.py @@ -40,7 +40,7 @@ class BezierCurve: for i in range(len(self.list_of_points)): # basis function for each i output_values.append( - comb(self.degree, i) * ((1 - t) ** (self.degree - i)) * (t ** i) + comb(self.degree, i) * ((1 - t) ** (self.degree - i)) * (t**i) ) # the basis must sum up to 1 for it to produce a valid Bezier curve. assert round(sum(output_values), 5) == 1 diff --git a/graphs/bidirectional_a_star.py b/graphs/bidirectional_a_star.py index 071f1cd68..373d67142 100644 --- a/graphs/bidirectional_a_star.py +++ b/graphs/bidirectional_a_star.py @@ -68,7 +68,7 @@ class Node: if HEURISTIC == 1: return abs(dx) + abs(dy) else: - return sqrt(dy ** 2 + dx ** 2) + return sqrt(dy**2 + dx**2) def __lt__(self, other: Node) -> bool: return self.f_cost < other.f_cost diff --git a/hashes/chaos_machine.py b/hashes/chaos_machine.py index 7ef4fdb3c..7ad3e5540 100644 --- a/hashes/chaos_machine.py +++ b/hashes/chaos_machine.py @@ -63,8 +63,8 @@ def pull(): params_space[key] = (machine_time * 0.01 + r * 1.01) % 1 + 3 # Choosing Chaotic Data - X = int(buffer_space[(key + 2) % m] * (10 ** 10)) - Y = int(buffer_space[(key - 2) % m] * (10 ** 10)) + X = int(buffer_space[(key + 2) % m] * (10**10)) + Y = int(buffer_space[(key - 2) % m] * (10**10)) # Machine Time machine_time += 1 diff --git a/hashes/hamming_code.py b/hashes/hamming_code.py index 4a32bae1a..ac20fe03b 100644 --- a/hashes/hamming_code.py +++ b/hashes/hamming_code.py @@ -78,7 +78,7 @@ def emitterConverter(sizePar, data): >>> emitterConverter(4, "101010111111") ['1', '1', '1', '1', '0', '1', '0', '0', '1', '0', '1', '1', '1', '1', '1', '1'] """ - if sizePar + len(data) <= 2 ** sizePar - (len(data) - 1): + if sizePar + len(data) <= 2**sizePar - (len(data) - 1): print("ERROR - size of parity don't match with size of data") exit(0) diff --git a/hashes/md5.py b/hashes/md5.py index b08ab9573..c56c073cc 100644 --- a/hashes/md5.py +++ b/hashes/md5.py @@ -94,7 +94,7 @@ def not32(i): def sum32(a, b): - return (a + b) % 2 ** 32 + return (a + b) % 2**32 def leftrot32(i, s): @@ -114,7 +114,7 @@ def md5me(testString): bs += format(ord(i), "08b") bs = pad(bs) - tvals = [int(2 ** 32 * abs(math.sin(i + 1))) for i in range(64)] + tvals = [int(2**32 * abs(math.sin(i + 1))) for i in range(64)] a0 = 0x67452301 b0 = 0xEFCDAB89 @@ -211,7 +211,7 @@ def md5me(testString): dtemp = D D = C C = B - B = sum32(B, leftrot32((A + f + tvals[i] + m[g]) % 2 ** 32, s[i])) + B = sum32(B, leftrot32((A + f + tvals[i] + m[g]) % 2**32, s[i])) A = dtemp a0 = sum32(a0, A) b0 = sum32(b0, B) diff --git a/linear_algebra/src/lib.py b/linear_algebra/src/lib.py index 85dc4b71c..2bfcea7f8 100644 --- a/linear_algebra/src/lib.py +++ b/linear_algebra/src/lib.py @@ -172,7 +172,7 @@ class Vector: """ if len(self.__components) == 0: raise Exception("Vector is empty") - squares = [c ** 2 for c in self.__components] + squares = [c**2 for c in self.__components] return math.sqrt(sum(squares)) def angle(self, other: Vector, deg: bool = False) -> float: diff --git a/machine_learning/k_means_clust.py b/machine_learning/k_means_clust.py index c45be8a4c..10c9374d8 100644 --- a/machine_learning/k_means_clust.py +++ b/machine_learning/k_means_clust.py @@ -112,7 +112,7 @@ def compute_heterogeneity(data, k, centroids, cluster_assignment): distances = pairwise_distances( member_data_points, [centroids[i]], metric="euclidean" ) - squared_distances = distances ** 2 + squared_distances = distances**2 heterogeneity += np.sum(squared_distances) return heterogeneity diff --git a/machine_learning/local_weighted_learning/local_weighted_learning.py b/machine_learning/local_weighted_learning/local_weighted_learning.py index af8694bf8..db6868687 100644 --- a/machine_learning/local_weighted_learning/local_weighted_learning.py +++ b/machine_learning/local_weighted_learning/local_weighted_learning.py @@ -25,7 +25,7 @@ def weighted_matrix(point: np.mat, training_data_x: np.mat, bandwidth: float) -> # calculating weights for all training examples [x(i)'s] for j in range(m): diff = point - training_data_x[j] - weights[j, j] = np.exp(diff * diff.T / (-2.0 * bandwidth ** 2)) + weights[j, j] = np.exp(diff * diff.T / (-2.0 * bandwidth**2)) return weights diff --git a/machine_learning/sequential_minimum_optimization.py b/machine_learning/sequential_minimum_optimization.py index 98ce05c46..c217a370a 100644 --- a/machine_learning/sequential_minimum_optimization.py +++ b/machine_learning/sequential_minimum_optimization.py @@ -345,15 +345,15 @@ class SmoSVM: ol = ( l1 * f1 + L * f2 - + 1 / 2 * l1 ** 2 * K(i1, i1) - + 1 / 2 * L ** 2 * K(i2, i2) + + 1 / 2 * l1**2 * K(i1, i1) + + 1 / 2 * L**2 * K(i2, i2) + s * L * l1 * K(i1, i2) ) oh = ( h1 * f1 + H * f2 - + 1 / 2 * h1 ** 2 * K(i1, i1) - + 1 / 2 * H ** 2 * K(i2, i2) + + 1 / 2 * h1**2 * K(i1, i1) + + 1 / 2 * H**2 * K(i2, i2) + s * H * h1 * K(i1, i2) ) """ diff --git a/maths/area.py b/maths/area.py index 7b39312cf..b1b139cf4 100644 --- a/maths/area.py +++ b/maths/area.py @@ -19,7 +19,7 @@ def surface_area_cube(side_length: float) -> float: """ if side_length < 0: raise ValueError("surface_area_cube() only accepts non-negative values") - return 6 * side_length ** 2 + return 6 * side_length**2 def surface_area_sphere(radius: float) -> float: @@ -39,7 +39,7 @@ def surface_area_sphere(radius: float) -> float: """ if radius < 0: raise ValueError("surface_area_sphere() only accepts non-negative values") - return 4 * pi * radius ** 2 + return 4 * pi * radius**2 def surface_area_hemisphere(radius: float) -> float: @@ -62,7 +62,7 @@ def surface_area_hemisphere(radius: float) -> float: """ if radius < 0: raise ValueError("surface_area_hemisphere() only accepts non-negative values") - return 3 * pi * radius ** 2 + return 3 * pi * radius**2 def surface_area_cone(radius: float, height: float) -> float: @@ -90,7 +90,7 @@ def surface_area_cone(radius: float, height: float) -> float: """ if radius < 0 or height < 0: raise ValueError("surface_area_cone() only accepts non-negative values") - return pi * radius * (radius + (height ** 2 + radius ** 2) ** 0.5) + return pi * radius * (radius + (height**2 + radius**2) ** 0.5) def surface_area_cylinder(radius: float, height: float) -> float: @@ -158,7 +158,7 @@ def area_square(side_length: float) -> float: """ if side_length < 0: raise ValueError("area_square() only accepts non-negative values") - return side_length ** 2 + return side_length**2 def area_triangle(base: float, height: float) -> float: @@ -307,7 +307,7 @@ def area_circle(radius: float) -> float: """ if radius < 0: raise ValueError("area_circle() only accepts non-negative values") - return pi * radius ** 2 + return pi * radius**2 def area_ellipse(radius_x: float, radius_y: float) -> float: diff --git a/maths/area_under_curve.py b/maths/area_under_curve.py index ce0932426..6fb3a7c98 100644 --- a/maths/area_under_curve.py +++ b/maths/area_under_curve.py @@ -50,7 +50,7 @@ def trapezoidal_area( if __name__ == "__main__": def f(x): - return x ** 3 + x ** 2 + return x**3 + x**2 print("f(x) = x^3 + x^2") print("The area between the curve, x = -5, x = 5 and the x axis is:") diff --git a/maths/armstrong_numbers.py b/maths/armstrong_numbers.py index 4e62737e1..65aebe937 100644 --- a/maths/armstrong_numbers.py +++ b/maths/armstrong_numbers.py @@ -36,7 +36,7 @@ def armstrong_number(n: int) -> bool: temp = n while temp > 0: rem = temp % 10 - sum += rem ** number_of_digits + sum += rem**number_of_digits temp //= 10 return n == sum @@ -63,7 +63,7 @@ def pluperfect_number(n: int) -> bool: digit_total += 1 for (cnt, i) in zip(digit_histogram, range(len(digit_histogram))): - sum += cnt * i ** digit_total + sum += cnt * i**digit_total return n == sum diff --git a/maths/basic_maths.py b/maths/basic_maths.py index 47d3d91b3..58e797772 100644 --- a/maths/basic_maths.py +++ b/maths/basic_maths.py @@ -81,14 +81,14 @@ def sum_of_divisors(n: int) -> int: temp += 1 n = int(n / 2) if temp > 1: - s *= (2 ** temp - 1) / (2 - 1) + s *= (2**temp - 1) / (2 - 1) for i in range(3, int(math.sqrt(n)) + 1, 2): temp = 1 while n % i == 0: temp += 1 n = int(n / i) if temp > 1: - s *= (i ** temp - 1) / (i - 1) + s *= (i**temp - 1) / (i - 1) return int(s) diff --git a/maths/binomial_distribution.py b/maths/binomial_distribution.py index a74a5a7ed..5b56f2d59 100644 --- a/maths/binomial_distribution.py +++ b/maths/binomial_distribution.py @@ -24,7 +24,7 @@ def binomial_distribution(successes: int, trials: int, prob: float) -> float: raise ValueError("the function is defined for non-negative integers") if not 0 < prob < 1: raise ValueError("prob has to be in range of 1 - 0") - probability = (prob ** successes) * ((1 - prob) ** (trials - successes)) + probability = (prob**successes) * ((1 - prob) ** (trials - successes)) # Calculate the binomial coefficient: n! / k!(n-k)! coefficient = float(factorial(trials)) coefficient /= factorial(successes) * factorial(trials - successes) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index ca4f4a236..07bd6d2ec 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -159,7 +159,7 @@ def fib_binet(n: int) -> list[int]: raise Exception("n is too large") sqrt_5 = sqrt(5) phi = (1 + sqrt_5) / 2 - return [round(phi ** i / sqrt_5) for i in range(n + 1)] + return [round(phi**i / sqrt_5) for i in range(n + 1)] if __name__ == "__main__": diff --git a/maths/gaussian.py b/maths/gaussian.py index a5dba50a9..51ebc2e25 100644 --- a/maths/gaussian.py +++ b/maths/gaussian.py @@ -52,7 +52,7 @@ def gaussian(x, mu: float = 0.0, sigma: float = 1.0) -> int: >>> gaussian(2523, mu=234234, sigma=3425) 0.0 """ - return 1 / sqrt(2 * pi * sigma ** 2) * exp(-((x - mu) ** 2) / (2 * sigma ** 2)) + return 1 / sqrt(2 * pi * sigma**2) * exp(-((x - mu) ** 2) / (2 * sigma**2)) if __name__ == "__main__": diff --git a/maths/karatsuba.py b/maths/karatsuba.py index df29c77a5..b772c0d77 100644 --- a/maths/karatsuba.py +++ b/maths/karatsuba.py @@ -14,8 +14,8 @@ def karatsuba(a, b): m1 = max(len(str(a)), len(str(b))) m2 = m1 // 2 - a1, a2 = divmod(a, 10 ** m2) - b1, b2 = divmod(b, 10 ** m2) + a1, a2 = divmod(a, 10**m2) + b1, b2 = divmod(b, 10**m2) x = karatsuba(a2, b2) y = karatsuba((a1 + a2), (b1 + b2)) diff --git a/maths/monte_carlo.py b/maths/monte_carlo.py index 28027cbe4..efb6a01d5 100644 --- a/maths/monte_carlo.py +++ b/maths/monte_carlo.py @@ -20,7 +20,7 @@ def pi_estimator(iterations: int): """ # A local function to see if a dot lands in the circle. def is_in_circle(x: float, y: float) -> bool: - distance_from_centre = sqrt((x ** 2) + (y ** 2)) + distance_from_centre = sqrt((x**2) + (y**2)) # Our circle has a radius of 1, so a distance # greater than 1 would land outside the circle. return distance_from_centre <= 1 diff --git a/maths/numerical_integration.py b/maths/numerical_integration.py index 577c41a44..cf2efce12 100644 --- a/maths/numerical_integration.py +++ b/maths/numerical_integration.py @@ -56,7 +56,7 @@ def trapezoidal_area( if __name__ == "__main__": def f(x): - return x ** 3 + return x**3 print("f(x) = x^3") print("The area between the curve, x = -10, x = 10 and the x axis is:") diff --git a/maths/perfect_square.py b/maths/perfect_square.py index 4393dcfbc..107e68528 100644 --- a/maths/perfect_square.py +++ b/maths/perfect_square.py @@ -58,9 +58,9 @@ def perfect_square_binary_search(n: int) -> bool: right = n while left <= right: mid = (left + right) // 2 - if mid ** 2 == n: + if mid**2 == n: return True - elif mid ** 2 > n: + elif mid**2 > n: right = mid - 1 else: left = mid + 1 diff --git a/maths/pi_monte_carlo_estimation.py b/maths/pi_monte_carlo_estimation.py index 20b46dddc..81be08378 100644 --- a/maths/pi_monte_carlo_estimation.py +++ b/maths/pi_monte_carlo_estimation.py @@ -11,7 +11,7 @@ class Point: True, if the point lies in the unit circle False, otherwise """ - return (self.x ** 2 + self.y ** 2) <= 1 + return (self.x**2 + self.y**2) <= 1 @classmethod def random_unit_square(cls): diff --git a/maths/polynomial_evaluation.py b/maths/polynomial_evaluation.py index 68ff97ddd..4e4016e51 100644 --- a/maths/polynomial_evaluation.py +++ b/maths/polynomial_evaluation.py @@ -12,7 +12,7 @@ def evaluate_poly(poly: Sequence[float], x: float) -> float: >>> evaluate_poly((0.0, 0.0, 5.0, 9.3, 7.0), 10.0) 79800.0 """ - return sum(c * (x ** i) for i, c in enumerate(poly)) + return sum(c * (x**i) for i, c in enumerate(poly)) def horner(poly: Sequence[float], x: float) -> float: diff --git a/maths/radix2_fft.py b/maths/radix2_fft.py index 9fc9f843e..0a431a115 100644 --- a/maths/radix2_fft.py +++ b/maths/radix2_fft.py @@ -91,7 +91,7 @@ class FFT: next_ncol = self.C_max_length // 2 while next_ncol > 0: new_dft = [[] for i in range(next_ncol)] - root = self.root ** next_ncol + root = self.root**next_ncol # First half of next step current_root = 1 diff --git a/maths/segmented_sieve.py b/maths/segmented_sieve.py index c1cc497ad..b15ec2480 100644 --- a/maths/segmented_sieve.py +++ b/maths/segmented_sieve.py @@ -48,4 +48,4 @@ def sieve(n): return prime -print(sieve(10 ** 6)) +print(sieve(10**6)) diff --git a/maths/sum_of_geometric_progression.py b/maths/sum_of_geometric_progression.py index f29dd8005..9079f35af 100644 --- a/maths/sum_of_geometric_progression.py +++ b/maths/sum_of_geometric_progression.py @@ -25,4 +25,4 @@ def sum_of_geometric_progression( return num_of_terms * first_term # Formula for finding sum of n terms of a GeometricProgression - return (first_term / (1 - common_ratio)) * (1 - common_ratio ** num_of_terms) + return (first_term / (1 - common_ratio)) * (1 - common_ratio**num_of_terms) diff --git a/physics/n_body_simulation.py b/physics/n_body_simulation.py index 045a49f7f..01083b9a2 100644 --- a/physics/n_body_simulation.py +++ b/physics/n_body_simulation.py @@ -159,16 +159,16 @@ class BodySystem: # Calculation of the distance using Pythagoras's theorem # Extra factor due to the softening technique - distance = (dif_x ** 2 + dif_y ** 2 + self.softening_factor) ** ( + distance = (dif_x**2 + dif_y**2 + self.softening_factor) ** ( 1 / 2 ) # Newton's law of universal gravitation. force_x += ( - self.gravitation_constant * body2.mass * dif_x / distance ** 3 + self.gravitation_constant * body2.mass * dif_x / distance**3 ) force_y += ( - self.gravitation_constant * body2.mass * dif_y / distance ** 3 + self.gravitation_constant * body2.mass * dif_y / distance**3 ) # Update the body's velocity once all the force components have been added diff --git a/project_euler/problem_006/sol1.py b/project_euler/problem_006/sol1.py index 61dd7a321..615991bb1 100644 --- a/project_euler/problem_006/sol1.py +++ b/project_euler/problem_006/sol1.py @@ -35,9 +35,9 @@ def solution(n: int = 100) -> int: sum_of_squares = 0 sum_of_ints = 0 for i in range(1, n + 1): - sum_of_squares += i ** 2 + sum_of_squares += i**2 sum_of_ints += i - return sum_of_ints ** 2 - sum_of_squares + return sum_of_ints**2 - sum_of_squares if __name__ == "__main__": diff --git a/project_euler/problem_007/sol2.py b/project_euler/problem_007/sol2.py index 20c2ddf21..dfcad8c14 100644 --- a/project_euler/problem_007/sol2.py +++ b/project_euler/problem_007/sol2.py @@ -25,7 +25,7 @@ def isprime(number: int) -> bool: True """ - for i in range(2, int(number ** 0.5) + 1): + for i in range(2, int(number**0.5) + 1): if number % i == 0: return False return True diff --git a/project_euler/problem_009/sol1.py b/project_euler/problem_009/sol1.py index 83c88acf1..1d908402b 100644 --- a/project_euler/problem_009/sol1.py +++ b/project_euler/problem_009/sol1.py @@ -33,7 +33,7 @@ def solution() -> int: for b in range(a + 1, 400): for c in range(b + 1, 500): if (a + b + c) == 1000: - if (a ** 2) + (b ** 2) == (c ** 2): + if (a**2) + (b**2) == (c**2): return a * b * c return -1 @@ -54,7 +54,7 @@ def solution_fast() -> int: for a in range(300): for b in range(400): c = 1000 - a - b - if a < b < c and (a ** 2) + (b ** 2) == (c ** 2): + if a < b < c and (a**2) + (b**2) == (c**2): return a * b * c return -1 diff --git a/project_euler/problem_010/sol3.py b/project_euler/problem_010/sol3.py index f49d9393c..72e2894df 100644 --- a/project_euler/problem_010/sol3.py +++ b/project_euler/problem_010/sol3.py @@ -46,7 +46,7 @@ def solution(n: int = 2000000) -> int: primality_list[0] = 1 primality_list[1] = 1 - for i in range(2, int(n ** 0.5) + 1): + for i in range(2, int(n**0.5) + 1): if primality_list[i] == 0: for j in range(i * i, n + 1, i): primality_list[j] = 1 diff --git a/project_euler/problem_016/sol1.py b/project_euler/problem_016/sol1.py index f6620aa94..93584d1d4 100644 --- a/project_euler/problem_016/sol1.py +++ b/project_euler/problem_016/sol1.py @@ -18,7 +18,7 @@ def solution(power: int = 1000) -> int: >>> solution(15) 26 """ - num = 2 ** power + num = 2**power string_num = str(num) list_num = list(string_num) sum_of_num = 0 @@ -31,6 +31,6 @@ def solution(power: int = 1000) -> int: if __name__ == "__main__": power = int(input("Enter the power of 2: ").strip()) - print("2 ^ ", power, " = ", 2 ** power) + print("2 ^ ", power, " = ", 2**power) result = solution(power) print("Sum of the digits is: ", result) diff --git a/project_euler/problem_016/sol2.py b/project_euler/problem_016/sol2.py index 304d27d1e..1408212e7 100644 --- a/project_euler/problem_016/sol2.py +++ b/project_euler/problem_016/sol2.py @@ -19,7 +19,7 @@ def solution(power: int = 1000) -> int: >>> solution(15) 26 """ - n = 2 ** power + n = 2**power r = 0 while n: r, n = r + n % 10, n // 10 diff --git a/project_euler/problem_023/sol1.py b/project_euler/problem_023/sol1.py index a72b6123e..83b85f3f7 100644 --- a/project_euler/problem_023/sol1.py +++ b/project_euler/problem_023/sol1.py @@ -30,7 +30,7 @@ def solution(limit=28123): """ sumDivs = [1] * (limit + 1) - for i in range(2, int(limit ** 0.5) + 1): + for i in range(2, int(limit**0.5) + 1): sumDivs[i * i] += i for k in range(i + 1, limit // i + 1): sumDivs[k * i] += k + i diff --git a/project_euler/problem_027/sol1.py b/project_euler/problem_027/sol1.py index 6f28b925b..928c0ec4f 100644 --- a/project_euler/problem_027/sol1.py +++ b/project_euler/problem_027/sol1.py @@ -61,7 +61,7 @@ def solution(a_limit: int = 1000, b_limit: int = 1000) -> int: if is_prime(b): count = 0 n = 0 - while is_prime((n ** 2) + (a * n) + b): + while is_prime((n**2) + (a * n) + b): count += 1 n += 1 if count > longest[0]: diff --git a/project_euler/problem_028/sol1.py b/project_euler/problem_028/sol1.py index cbc7de6be..1ea5d4fca 100644 --- a/project_euler/problem_028/sol1.py +++ b/project_euler/problem_028/sol1.py @@ -40,7 +40,7 @@ def solution(n: int = 1001) -> int: for i in range(1, int(ceil(n / 2.0))): odd = 2 * i + 1 even = 2 * i - total = total + 4 * odd ** 2 - 6 * even + total = total + 4 * odd**2 - 6 * even return total diff --git a/project_euler/problem_029/sol1.py b/project_euler/problem_029/sol1.py index 726bcaf6e..d3ab90ac7 100644 --- a/project_euler/problem_029/sol1.py +++ b/project_euler/problem_029/sol1.py @@ -41,7 +41,7 @@ def solution(n: int = 100) -> int: for a in range(2, N): for b in range(2, N): - currentPow = a ** b # calculates the current power + currentPow = a**b # calculates the current power collectPowers.add(currentPow) # adds the result to the set return len(collectPowers) diff --git a/project_euler/problem_048/sol1.py b/project_euler/problem_048/sol1.py index 01ff702d9..5a4538cf5 100644 --- a/project_euler/problem_048/sol1.py +++ b/project_euler/problem_048/sol1.py @@ -17,7 +17,7 @@ def solution(): """ total = 0 for i in range(1, 1001): - total += i ** i + total += i**i return str(total)[-10:] diff --git a/project_euler/problem_050/sol1.py b/project_euler/problem_050/sol1.py index cfb1911df..fc6e6f2b9 100644 --- a/project_euler/problem_050/sol1.py +++ b/project_euler/problem_050/sol1.py @@ -35,7 +35,7 @@ def prime_sieve(limit: int) -> list[int]: is_prime[1] = False is_prime[2] = True - for i in range(3, int(limit ** 0.5 + 1), 2): + for i in range(3, int(limit**0.5 + 1), 2): index = i * 2 while index < limit: is_prime[index] = False diff --git a/project_euler/problem_051/sol1.py b/project_euler/problem_051/sol1.py index eedb02379..921704bc4 100644 --- a/project_euler/problem_051/sol1.py +++ b/project_euler/problem_051/sol1.py @@ -37,7 +37,7 @@ def prime_sieve(n: int) -> list[int]: is_prime[1] = False is_prime[2] = True - for i in range(3, int(n ** 0.5 + 1), 2): + for i in range(3, int(n**0.5 + 1), 2): index = i * 2 while index < n: is_prime[index] = False diff --git a/project_euler/problem_056/sol1.py b/project_euler/problem_056/sol1.py index f1ec03c49..c772bec58 100644 --- a/project_euler/problem_056/sol1.py +++ b/project_euler/problem_056/sol1.py @@ -30,7 +30,7 @@ def solution(a: int = 100, b: int = 100) -> int: # RETURN the MAXIMUM from the list of SUMs of the list of INT converted from STR of # BASE raised to the POWER return max( - sum(int(x) for x in str(base ** power)) + sum(int(x) for x in str(base**power)) for base in range(a) for power in range(b) ) diff --git a/project_euler/problem_062/sol1.py b/project_euler/problem_062/sol1.py index 83286c801..0c9baf880 100644 --- a/project_euler/problem_062/sol1.py +++ b/project_euler/problem_062/sol1.py @@ -55,7 +55,7 @@ def get_digits(num: int) -> str: >>> get_digits(123) '0166788' """ - return "".join(sorted(list(str(num ** 3)))) + return "".join(sorted(list(str(num**3)))) if __name__ == "__main__": diff --git a/project_euler/problem_063/sol1.py b/project_euler/problem_063/sol1.py index 29efddba4..bea30a2e5 100644 --- a/project_euler/problem_063/sol1.py +++ b/project_euler/problem_063/sol1.py @@ -26,7 +26,7 @@ def solution(max_base: int = 10, max_power: int = 22) -> int: bases = range(1, max_base) powers = range(1, max_power) return sum( - 1 for power in powers for base in bases if len(str(base ** power)) == power + 1 for power in powers for base in bases if len(str(base**power)) == power ) diff --git a/project_euler/problem_064/sol1.py b/project_euler/problem_064/sol1.py index 69e3f6d97..5df64a90a 100644 --- a/project_euler/problem_064/sol1.py +++ b/project_euler/problem_064/sol1.py @@ -38,7 +38,7 @@ def continuous_fraction_period(n: int) -> int: period = 0 while integer_part != 2 * ROOT: numerator = denominator * integer_part - numerator - denominator = (n - numerator ** 2) / denominator + denominator = (n - numerator**2) / denominator integer_part = int((ROOT + numerator) / denominator) period += 1 return period diff --git a/project_euler/problem_069/sol1.py b/project_euler/problem_069/sol1.py index d148dd79a..5dfd61a89 100644 --- a/project_euler/problem_069/sol1.py +++ b/project_euler/problem_069/sol1.py @@ -24,7 +24,7 @@ Find the value of n ≤ 1,000,000 for which n/φ(n) is a maximum. """ -def solution(n: int = 10 ** 6) -> int: +def solution(n: int = 10**6) -> int: """ Returns solution to problem. Algorithm: diff --git a/project_euler/problem_077/sol1.py b/project_euler/problem_077/sol1.py index 214e25879..6098ea9e5 100644 --- a/project_euler/problem_077/sol1.py +++ b/project_euler/problem_077/sol1.py @@ -23,7 +23,7 @@ primes = set(range(3, NUM_PRIMES, 2)) primes.add(2) prime: int -for prime in range(3, ceil(NUM_PRIMES ** 0.5), 2): +for prime in range(3, ceil(NUM_PRIMES**0.5), 2): if prime not in primes: continue primes.difference_update(set(range(prime * prime, NUM_PRIMES, prime))) diff --git a/project_euler/problem_086/sol1.py b/project_euler/problem_086/sol1.py index 0bf66e6b5..064af215c 100644 --- a/project_euler/problem_086/sol1.py +++ b/project_euler/problem_086/sol1.py @@ -91,7 +91,7 @@ def solution(limit: int = 1000000) -> int: while num_cuboids <= limit: max_cuboid_size += 1 for sum_shortest_sides in range(2, 2 * max_cuboid_size + 1): - if sqrt(sum_shortest_sides ** 2 + max_cuboid_size ** 2).is_integer(): + if sqrt(sum_shortest_sides**2 + max_cuboid_size**2).is_integer(): num_cuboids += ( min(max_cuboid_size, sum_shortest_sides // 2) - max(1, sum_shortest_sides - max_cuboid_size) diff --git a/project_euler/problem_092/sol1.py b/project_euler/problem_092/sol1.py index 437a85bad..d326fc33f 100644 --- a/project_euler/problem_092/sol1.py +++ b/project_euler/problem_092/sol1.py @@ -12,7 +12,7 @@ How many starting numbers below ten million will arrive at 89? """ -DIGITS_SQUARED = [digit ** 2 for digit in range(10)] +DIGITS_SQUARED = [digit**2 for digit in range(10)] def next_number(number: int) -> int: diff --git a/project_euler/problem_097/sol1.py b/project_euler/problem_097/sol1.py index 2e848c09a..da5e8120b 100644 --- a/project_euler/problem_097/sol1.py +++ b/project_euler/problem_097/sol1.py @@ -34,7 +34,7 @@ def solution(n: int = 10) -> str: """ if not isinstance(n, int) or n < 0: raise ValueError("Invalid input") - MODULUS = 10 ** n + MODULUS = 10**n NUMBER = 28433 * (pow(2, 7830457, MODULUS)) + 1 return str(NUMBER % MODULUS) diff --git a/project_euler/problem_101/sol1.py b/project_euler/problem_101/sol1.py index 14013c435..046788475 100644 --- a/project_euler/problem_101/sol1.py +++ b/project_euler/problem_101/sol1.py @@ -175,15 +175,15 @@ def question_function(variable: int) -> int: return ( 1 - variable - + variable ** 2 - - variable ** 3 - + variable ** 4 - - variable ** 5 - + variable ** 6 - - variable ** 7 - + variable ** 8 - - variable ** 9 - + variable ** 10 + + variable**2 + - variable**3 + + variable**4 + - variable**5 + + variable**6 + - variable**7 + + variable**8 + - variable**9 + + variable**10 ) diff --git a/project_euler/problem_125/sol1.py b/project_euler/problem_125/sol1.py index afc1f2890..7a8f908ed 100644 --- a/project_euler/problem_125/sol1.py +++ b/project_euler/problem_125/sol1.py @@ -35,7 +35,7 @@ def solution() -> int: Returns the sum of all numbers less than 1e8 that are both palindromic and can be written as the sum of consecutive squares. """ - LIMIT = 10 ** 8 + LIMIT = 10**8 answer = set() first_square = 1 sum_squares = 5 @@ -45,9 +45,9 @@ def solution() -> int: if is_palindrome(sum_squares): answer.add(sum_squares) last_square += 1 - sum_squares += last_square ** 2 + sum_squares += last_square**2 first_square += 1 - sum_squares = first_square ** 2 + (first_square + 1) ** 2 + sum_squares = first_square**2 + (first_square + 1) ** 2 return sum(answer) diff --git a/project_euler/problem_144/sol1.py b/project_euler/problem_144/sol1.py index 3f7a766be..b5f103b64 100644 --- a/project_euler/problem_144/sol1.py +++ b/project_euler/problem_144/sol1.py @@ -58,15 +58,15 @@ def next_point( # y^2 + 4x^2 = 100 # y - b = m * (x - a) # ==> A x^2 + B x + C = 0 - quadratic_term = outgoing_gradient ** 2 + 4 + quadratic_term = outgoing_gradient**2 + 4 linear_term = 2 * outgoing_gradient * (point_y - outgoing_gradient * point_x) constant_term = (point_y - outgoing_gradient * point_x) ** 2 - 100 x_minus = ( - -linear_term - sqrt(linear_term ** 2 - 4 * quadratic_term * constant_term) + -linear_term - sqrt(linear_term**2 - 4 * quadratic_term * constant_term) ) / (2 * quadratic_term) x_plus = ( - -linear_term + sqrt(linear_term ** 2 - 4 * quadratic_term * constant_term) + -linear_term + sqrt(linear_term**2 - 4 * quadratic_term * constant_term) ) / (2 * quadratic_term) # two solutions, one of which is our input point diff --git a/project_euler/problem_145/__init__.py b/project_euler/problem_145/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/project_euler/problem_145/sol1.py b/project_euler/problem_145/sol1.py deleted file mode 100644 index 5ba3af86a..000000000 --- a/project_euler/problem_145/sol1.py +++ /dev/null @@ -1,87 +0,0 @@ -""" -Problem 145: https://projecteuler.net/problem=145 - -Name: How many reversible numbers are there below one-billion? - -Some positive integers n have the property that the -sum [ n + reverse(n) ] consists entirely of odd (decimal) digits. -For instance, 36 + 63 = 99 and 409 + 904 = 1313. -We will call such numbers reversible; so 36, 63, 409, and 904 are reversible. -Leading zeroes are not allowed in either n or reverse(n). - -There are 120 reversible numbers below one-thousand. - -How many reversible numbers are there below one-billion (10^9)? - - -Solution: - -Here a brute force solution is used to find and count the reversible numbers. - -""" -from __future__ import annotations - - -def check_if_odd(sum: int = 36) -> int: - """ - Check if the last digit in the sum is even or odd. If even return 0. - If odd then floor division by 10 is used to remove the last number. - Process continues until sum becomes 0 because no more numbers. - >>> check_if_odd(36) - 0 - >>> check_if_odd(33) - 1 - """ - while sum > 0: - if (sum % 10) % 2 == 0: - return 0 - sum = sum // 10 - return 1 - - -def find_reverse_number(number: int = 36) -> int: - """ - Reverses the given number. Does not work with number that end in zero. - >>> find_reverse_number(36) - 63 - >>> find_reverse_number(409) - 904 - """ - reverse = 0 - - while number > 0: - temp = number % 10 - reverse = reverse * 10 + temp - number = number // 10 - - return reverse - - -def solution(number: int = 1000000000) -> int: - """ - Loops over the range of numbers. - Checks if they have ending zeros which disqualifies them from being reversible. - If that condition is passed it generates the reversed number. - Then sum up n and reverse(n). - Then check if all the numbers in the sum are odd. If true add to the answer. - >>> solution(1000000000) - 608720 - >>> solution(1000000) - 18720 - >>> solution(1000000) - 18720 - >>> solution(1000) - 120 - """ - answer = 0 - for x in range(1, number): - if x % 10 != 0: - reversed_number = find_reverse_number(x) - sum = x + reversed_number - answer += check_if_odd(sum) - - return answer - - -if __name__ == "__main__": - print(f"{solution() = }") diff --git a/project_euler/problem_173/sol1.py b/project_euler/problem_173/sol1.py index d539b1437..5416e2546 100644 --- a/project_euler/problem_173/sol1.py +++ b/project_euler/problem_173/sol1.py @@ -25,8 +25,8 @@ def solution(limit: int = 1000000) -> int: answer = 0 for outer_width in range(3, (limit // 4) + 2): - if outer_width ** 2 > limit: - hole_width_lower_bound = max(ceil(sqrt(outer_width ** 2 - limit)), 1) + if outer_width**2 > limit: + hole_width_lower_bound = max(ceil(sqrt(outer_width**2 - limit)), 1) else: hole_width_lower_bound = 1 if (outer_width - hole_width_lower_bound) % 2: diff --git a/project_euler/problem_180/sol1.py b/project_euler/problem_180/sol1.py index f7c097323..12e34dcaa 100644 --- a/project_euler/problem_180/sol1.py +++ b/project_euler/problem_180/sol1.py @@ -61,7 +61,7 @@ def is_sq(number: int) -> bool: >>> is_sq(1000000) True """ - sq: int = int(number ** 0.5) + sq: int = int(number**0.5) return number == sq * sq diff --git a/project_euler/problem_188/sol1.py b/project_euler/problem_188/sol1.py index c8cd9eb10..dd4360adb 100644 --- a/project_euler/problem_188/sol1.py +++ b/project_euler/problem_188/sol1.py @@ -59,7 +59,7 @@ def solution(base: int = 1777, height: int = 1855, digits: int = 8) -> int: # exponentiation result = base for i in range(1, height): - result = _modexpt(base, result, 10 ** digits) + result = _modexpt(base, result, 10**digits) return result diff --git a/project_euler/problem_203/sol1.py b/project_euler/problem_203/sol1.py index fe4d14b20..2ba3c96c9 100644 --- a/project_euler/problem_203/sol1.py +++ b/project_euler/problem_203/sol1.py @@ -82,10 +82,10 @@ def get_primes_squared(max_number: int) -> list[int]: if non_primes[num]: continue - for num_counter in range(num ** 2, max_prime + 1, num): + for num_counter in range(num**2, max_prime + 1, num): non_primes[num_counter] = True - primes.append(num ** 2) + primes.append(num**2) return primes diff --git a/project_euler/problem_205/sol1.py b/project_euler/problem_205/sol1.py index 7249df488..63b997b9f 100644 --- a/project_euler/problem_205/sol1.py +++ b/project_euler/problem_205/sol1.py @@ -63,7 +63,7 @@ def solution() -> float: colin_totals_frequencies[min_colin_total:peter_total] ) - total_games_number = (4 ** 9) * (6 ** 6) + total_games_number = (4**9) * (6**6) peter_win_probability = peter_wins_count / total_games_number rounded_peter_win_probability = round(peter_win_probability, ndigits=7) diff --git a/project_euler/problem_207/sol1.py b/project_euler/problem_207/sol1.py index 99d1a9174..2b3591f51 100644 --- a/project_euler/problem_207/sol1.py +++ b/project_euler/problem_207/sol1.py @@ -81,7 +81,7 @@ def solution(max_proportion: float = 1 / 12345) -> int: integer = 3 while True: - partition_candidate = (integer ** 2 - 1) / 4 + partition_candidate = (integer**2 - 1) / 4 # if candidate is an integer, then there is a partition for k if partition_candidate == int(partition_candidate): partition_candidate = int(partition_candidate) diff --git a/project_euler/problem_234/sol1.py b/project_euler/problem_234/sol1.py index 7516b164d..7b20a2206 100644 --- a/project_euler/problem_234/sol1.py +++ b/project_euler/problem_234/sol1.py @@ -35,7 +35,7 @@ def prime_sieve(n: int) -> list: is_prime[1] = False is_prime[2] = True - for i in range(3, int(n ** 0.5 + 1), 2): + for i in range(3, int(n**0.5 + 1), 2): index = i * 2 while index < n: is_prime[index] = False @@ -69,11 +69,11 @@ def solution(limit: int = 999_966_663_333) -> int: prime_index = 0 last_prime = primes[prime_index] - while (last_prime ** 2) <= limit: + while (last_prime**2) <= limit: next_prime = primes[prime_index + 1] - lower_bound = last_prime ** 2 - upper_bound = next_prime ** 2 + lower_bound = last_prime**2 + upper_bound = next_prime**2 # Get numbers divisible by lps(current) current = lower_bound + last_prime diff --git a/project_euler/problem_301/sol1.py b/project_euler/problem_301/sol1.py index b1d434c18..4b494033c 100644 --- a/project_euler/problem_301/sol1.py +++ b/project_euler/problem_301/sol1.py @@ -48,8 +48,8 @@ def solution(exponent: int = 30) -> int: # To find how many total games were lost for a given exponent x, # we need to find the Fibonacci number F(x+2). fibonacci_index = exponent + 2 - phi = (1 + 5 ** 0.5) / 2 - fibonacci = (phi ** fibonacci_index - (phi - 1) ** fibonacci_index) / 5 ** 0.5 + phi = (1 + 5**0.5) / 2 + fibonacci = (phi**fibonacci_index - (phi - 1) ** fibonacci_index) / 5**0.5 return int(fibonacci) diff --git a/project_euler/problem_551/sol1.py b/project_euler/problem_551/sol1.py index 005d2e985..c15445e4d 100644 --- a/project_euler/problem_551/sol1.py +++ b/project_euler/problem_551/sol1.py @@ -14,7 +14,7 @@ Find a(10^15) ks = [k for k in range(2, 20 + 1)] -base = [10 ** k for k in range(ks[-1] + 1)] +base = [10**k for k in range(ks[-1] + 1)] memo: dict[int, dict[int, list[list[int]]]] = {} @@ -168,7 +168,7 @@ def add(digits, k, addend): digits.append(digit) -def solution(n: int = 10 ** 15) -> int: +def solution(n: int = 10**15) -> int: """ returns n-th term of sequence @@ -193,7 +193,7 @@ def solution(n: int = 10 ** 15) -> int: a_n = 0 for j in range(len(digits)): - a_n += digits[j] * 10 ** j + a_n += digits[j] * 10**j return a_n diff --git a/quantum/deutsch_jozsa.py b/quantum/deutsch_jozsa.py index 304eea196..d7e2d8335 100755 --- a/quantum/deutsch_jozsa.py +++ b/quantum/deutsch_jozsa.py @@ -39,7 +39,7 @@ def dj_oracle(case: str, num_qubits: int) -> q.QuantumCircuit: if case == "balanced": # First generate a random number that tells us which CNOTs to # wrap in X-gates: - b = np.random.randint(1, 2 ** num_qubits) + b = np.random.randint(1, 2**num_qubits) # Next, format 'b' as a binary string of length 'n', padded with zeros: b_str = format(b, f"0{num_qubits}b") # Next, we place the first X-gates. Each digit in our binary string diff --git a/searches/hill_climbing.py b/searches/hill_climbing.py index bb24e781a..83a3b8b74 100644 --- a/searches/hill_climbing.py +++ b/searches/hill_climbing.py @@ -166,7 +166,7 @@ if __name__ == "__main__": doctest.testmod() def test_f1(x, y): - return (x ** 2) + (y ** 2) + return (x**2) + (y**2) # starting the problem with initial coordinates (3, 4) prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) @@ -187,7 +187,7 @@ if __name__ == "__main__": ) def test_f2(x, y): - return (3 * x ** 2) - (6 * y) + return (3 * x**2) - (6 * y) prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) local_min = hill_climbing(prob, find_max=True) diff --git a/searches/simulated_annealing.py b/searches/simulated_annealing.py index ad29559f1..063d225d0 100644 --- a/searches/simulated_annealing.py +++ b/searches/simulated_annealing.py @@ -97,7 +97,7 @@ def simulated_annealing( if __name__ == "__main__": def test_f1(x, y): - return (x ** 2) + (y ** 2) + return (x**2) + (y**2) # starting the problem with initial coordinates (12, 47) prob = SearchProblem(x=12, y=47, step_size=1, function_to_optimize=test_f1) @@ -120,7 +120,7 @@ if __name__ == "__main__": ) def test_f2(x, y): - return (3 * x ** 2) - (6 * y) + return (3 * x**2) - (6 * y) prob = SearchProblem(x=3, y=4, step_size=1, function_to_optimize=test_f1) local_min = simulated_annealing(prob, find_max=False, visualization=True)