mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 17:34:49 +08:00
Set the Python file maximum line length to 88 characters (#2122)
* flake8 --max-line-length=88 * fixup! Format Python code with psf/black push Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
@ -4,7 +4,8 @@ def bailey_borwein_plouffe(digit_position: int, precision: int = 1000) -> str:
|
||||
Bailey-Borwein-Plouffe (BBP) formula to calculate the nth hex digit of pi.
|
||||
Wikipedia page:
|
||||
https://en.wikipedia.org/wiki/Bailey%E2%80%93Borwein%E2%80%93Plouffe_formula
|
||||
@param digit_position: a positive integer representing the position of the digit to extract.
|
||||
@param digit_position: a positive integer representing the position of the digit to
|
||||
extract.
|
||||
The digit immediately after the decimal point is located at position 1.
|
||||
@param precision: number of terms in the second summation to calculate.
|
||||
A higher number reduces the chance of an error but increases the runtime.
|
||||
@ -41,7 +42,8 @@ def bailey_borwein_plouffe(digit_position: int, precision: int = 1000) -> str:
|
||||
elif (not isinstance(precision, int)) or (precision < 0):
|
||||
raise ValueError("Precision must be a nonnegative integer")
|
||||
|
||||
# compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly accurate
|
||||
# compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
|
||||
# accurate
|
||||
sum_result = (
|
||||
4 * _subsum(digit_position, 1, precision)
|
||||
- 2 * _subsum(digit_position, 4, precision)
|
||||
@ -70,8 +72,9 @@ def _subsum(
|
||||
denominator = 8 * sum_index + denominator_addend
|
||||
exponential_term = 0.0
|
||||
if sum_index < digit_pos_to_extract:
|
||||
# if the exponential term is an integer and we mod it by the denominator before
|
||||
# dividing, only the integer part of the sum will change; the fractional part will not
|
||||
# if the exponential term is an integer and we mod it by the denominator
|
||||
# before dividing, only the integer part of the sum will change;
|
||||
# the fractional part will not
|
||||
exponential_term = pow(
|
||||
16, digit_pos_to_extract - 1 - sum_index, denominator
|
||||
)
|
||||
|
@ -6,7 +6,8 @@ def ceil(x) -> int:
|
||||
:return: the smallest integer >= x.
|
||||
|
||||
>>> import math
|
||||
>>> all(ceil(n) == math.ceil(n) for n in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000))
|
||||
>>> all(ceil(n) == math.ceil(n) for n
|
||||
... in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000))
|
||||
True
|
||||
"""
|
||||
return (
|
||||
|
@ -1,5 +1,6 @@
|
||||
# Python program to show the usage of Fermat's little theorem in a division
|
||||
# According to Fermat's little theorem, (a / b) mod p always equals a * (b ^ (p - 2)) mod p
|
||||
# According to Fermat's little theorem, (a / b) mod p always equals
|
||||
# a * (b ^ (p - 2)) mod p
|
||||
# Here we assume that p is a prime number, b divides a, and p doesn't divide b
|
||||
# Wikipedia reference: https://en.wikipedia.org/wiki/Fermat%27s_little_theorem
|
||||
|
||||
|
@ -4,7 +4,8 @@
|
||||
|
||||
2. Calculates the fibonacci sequence with a formula
|
||||
an = [ Phin - (phi)n ]/Sqrt[5]
|
||||
reference-->Su, Francis E., et al. "Fibonacci Number Formula." Math Fun Facts. <http://www.math.hmc.edu/funfacts>
|
||||
reference-->Su, Francis E., et al. "Fibonacci Number Formula." Math Fun Facts.
|
||||
<http://www.math.hmc.edu/funfacts>
|
||||
"""
|
||||
import math
|
||||
import functools
|
||||
@ -71,11 +72,13 @@ def _check_number_input(n, min_thresh, max_thresh=None):
|
||||
print("Incorrect Input: number must not be less than 0")
|
||||
except ValueTooSmallError:
|
||||
print(
|
||||
f"Incorrect Input: input number must be > {min_thresh} for the recursive calculation"
|
||||
f"Incorrect Input: input number must be > {min_thresh} for the recursive "
|
||||
"calculation"
|
||||
)
|
||||
except ValueTooLargeError:
|
||||
print(
|
||||
f"Incorrect Input: input number must be < {max_thresh} for the recursive calculation"
|
||||
f"Incorrect Input: input number must be < {max_thresh} for the recursive "
|
||||
"calculation"
|
||||
)
|
||||
return False
|
||||
|
||||
|
@ -6,7 +6,8 @@ def floor(x) -> int:
|
||||
:return: the largest integer <= x.
|
||||
|
||||
>>> import math
|
||||
>>> all(floor(n) == math.floor(n) for n in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000))
|
||||
>>> all(floor(n) == math.floor(n) for n
|
||||
... in (1, -1, 0, -0, 1.1, -1.1, 1.0, -1.0, 1_000_000_000))
|
||||
True
|
||||
"""
|
||||
return (
|
||||
|
@ -8,7 +8,8 @@ def gamma(num: float) -> float:
|
||||
https://en.wikipedia.org/wiki/Gamma_function
|
||||
In mathematics, the gamma function is one commonly
|
||||
used extension of the factorial function to complex numbers.
|
||||
The gamma function is defined for all complex numbers except the non-positive integers
|
||||
The gamma function is defined for all complex numbers except the non-positive
|
||||
integers
|
||||
|
||||
|
||||
>>> gamma(-1)
|
||||
|
@ -25,9 +25,9 @@ def greatest_common_divisor(a, b):
|
||||
|
||||
|
||||
"""
|
||||
Below method is more memory efficient because it does not use the stack (chunk of memory).
|
||||
While above method is good, uses more memory for huge numbers because of the recursive calls
|
||||
required to calculate the greatest common divisor.
|
||||
Below method is more memory efficient because it does not use the stack (chunk of
|
||||
memory). While above method is good, uses more memory for huge numbers because of the
|
||||
recursive calls required to calculate the greatest common divisor.
|
||||
"""
|
||||
|
||||
|
||||
@ -50,7 +50,8 @@ def main():
|
||||
num_1 = int(nums[0])
|
||||
num_2 = int(nums[1])
|
||||
print(
|
||||
f"greatest_common_divisor({num_1}, {num_2}) = {greatest_common_divisor(num_1, num_2)}"
|
||||
f"greatest_common_divisor({num_1}, {num_2}) = "
|
||||
f"{greatest_common_divisor(num_1, num_2)}"
|
||||
)
|
||||
print(f"By iterative gcd({num_1}, {num_2}) = {gcd_by_iterative(num_1, num_2)}")
|
||||
except (IndexError, UnboundLocalError, ValueError):
|
||||
|
@ -1,6 +1,6 @@
|
||||
"""
|
||||
In mathematics, the Lucas–Lehmer test (LLT) is a primality test for Mersenne numbers.
|
||||
https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test
|
||||
In mathematics, the Lucas–Lehmer test (LLT) is a primality test for Mersenne
|
||||
numbers. https://en.wikipedia.org/wiki/Lucas%E2%80%93Lehmer_primality_test
|
||||
|
||||
A Mersenne number is a number that is one less than a power of two.
|
||||
That is M_p = 2^p - 1
|
||||
|
@ -15,7 +15,7 @@ class Matrix:
|
||||
if isinstance(arg, list): # Initializes a matrix identical to the one provided.
|
||||
self.t = arg
|
||||
self.n = len(arg)
|
||||
else: # Initializes a square matrix of the given size and set the values to zero.
|
||||
else: # Initializes a square matrix of the given size and set values to zero.
|
||||
self.n = arg
|
||||
self.t = [[0 for _ in range(self.n)] for _ in range(self.n)]
|
||||
|
||||
|
@ -1,7 +1,8 @@
|
||||
"""
|
||||
Modular Exponential.
|
||||
Modular exponentiation is a type of exponentiation performed over a modulus.
|
||||
For more explanation, please check https://en.wikipedia.org/wiki/Modular_exponentiation
|
||||
For more explanation, please check
|
||||
https://en.wikipedia.org/wiki/Modular_exponentiation
|
||||
"""
|
||||
|
||||
"""Calculate Modular Exponential."""
|
||||
|
@ -44,7 +44,8 @@ if __name__ == "__main__":
|
||||
Example:
|
||||
>>> poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7.0x^4 + 9.3x^3 + 5.0x^2
|
||||
>>> x = -13.0
|
||||
>>> print(evaluate_poly(poly, x)) # f(-13) = 7.0(-13)^4 + 9.3(-13)^3 + 5.0(-13)^2 = 180339.9
|
||||
>>> # f(-13) = 7.0(-13)^4 + 9.3(-13)^3 + 5.0(-13)^2 = 180339.9
|
||||
>>> print(evaluate_poly(poly, x))
|
||||
180339.9
|
||||
"""
|
||||
poly = (0.0, 0.0, 5.0, 9.3, 7.0)
|
||||
|
@ -1,7 +1,8 @@
|
||||
"""
|
||||
This script demonstrates the implementation of the ReLU function.
|
||||
|
||||
It's a kind of activation function defined as the positive part of its argument in the context of neural network.
|
||||
It's a kind of activation function defined as the positive part of its argument in the
|
||||
context of neural network.
|
||||
The function takes a vector of K real numbers as input and then argmax(x, 0).
|
||||
After through ReLU, the element of the vector always 0 or real number.
|
||||
|
||||
|
@ -1,8 +1,10 @@
|
||||
"""
|
||||
Sieve of Eratosthones
|
||||
|
||||
The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or equal to a given value.
|
||||
Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
||||
The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or
|
||||
equal to a given value.
|
||||
Illustration:
|
||||
https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
||||
Reference: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
|
||||
|
||||
doctest provider: Bruno Simas Hadlich (https://github.com/brunohadlich)
|
||||
|
@ -25,7 +25,8 @@ def square_root_iterative(
|
||||
Square root is aproximated using Newtons method.
|
||||
https://en.wikipedia.org/wiki/Newton%27s_method
|
||||
|
||||
>>> all(abs(square_root_iterative(i)-math.sqrt(i)) <= .00000000000001 for i in range(0, 500))
|
||||
>>> all(abs(square_root_iterative(i)-math.sqrt(i)) <= .00000000000001
|
||||
... for i in range(500))
|
||||
True
|
||||
|
||||
>>> square_root_iterative(-1)
|
||||
|
Reference in New Issue
Block a user