src.pydasa.structs.tools.math#

Module with math functions for finding prime numbers and calculating factorials. in PyDASA.

Module with math functions for handling data in the for Separate Chaining Hash Table.

IMPORTANT: based on the implementations proposed by the following authors/books:

  1. Algorithms, 4th Edition, Robert Sedgewick and Kevin Wayne.

  2. Data Structure and Algorithms in Python, M.T. Goodrich, R. Tamassia, M.H. Goldwasser.

NOTE: code contributed by Sanjit_Prasad in https://www.geeksforgeeks.org/prime-numbers/

Functions#

is_prime(n)

is_prime() checks if a number is prime or not. Original code from Sanjit_Prasad.

next_prime(n)

next_prime() returns the next prime number greater than n.

previous_prime(n)

previous_prime() returns the previous prime number less than n.

gfactorial(x[, prec])

gfactorial() calculates the factorial of a number, including support for floats less than 1.0.

Module Contents#

src.pydasa.structs.tools.math.is_prime(n)#

is_prime() checks if a number is prime or not. Original code from Sanjit_Prasad.

Parameters:

n (int) – number to check if it is prime.

Returns:

True if the number is prime, False otherwise.

Return type:

bool

src.pydasa.structs.tools.math.next_prime(n)#

next_prime() returns the next prime number greater than n.

Parameters:

n (int) – number to check if it is prime.

Returns:

the next prime number greater than n.

Return type:

int

src.pydasa.structs.tools.math.previous_prime(n)#

previous_prime() returns the previous prime number less than n.

Parameters:

n (int) – number to check if it is prime.

Returns:

the previous prime number less than n.

Return type:

int

src.pydasa.structs.tools.math.gfactorial(x, prec=None)#

gfactorial() calculates the factorial of a number, including support for floats less than 1.0.

  • For integers n ≥ 0: Returns n! (n factorial).

  • For floats x: Returns Γ(x+1) (gamma function).

Parameters:
  • x (Union[int, float]) – The number to compute the factorial for.

  • prec (Optional[int], optional) – precision, or the number of decimal places to round the result to. Defaults to None.

Raises:

ValueError – If x is a negative integer.

Returns:

The factorial of x. Returns an integer for integer inputs ≥ 0, and a float for float inputs or integers < 0.

Return type:

Union[int, float]

Examples

>>> gfactorial(5)
120
>>> gfactorial(0)
1
>>> gfactorial(0.5)  # Equivalent to Γ(1.5) = 0.5 * Γ(0.5) = 0.5 * √Pi
0.8862269254527579
>>> gfactorial(-0.5)  # Equivalent to Γ(0.5) = √Pi
1.7724538509055159