maths.gcd_of_n_numbers¶
Gcd of N Numbers Reference: https://en.wikipedia.org/wiki/Greatest_common_divisor
Functions¶
|
this is a recursive function for get all factors of number |
|
get gcd of n numbers: |
Module Contents¶
- maths.gcd_of_n_numbers.get_factors(number: int, factors: collections.Counter | None = None, factor: int = 2) collections.Counter ¶
this is a recursive function for get all factors of number >>> get_factors(45) Counter({3: 2, 5: 1}) >>> get_factors(2520) Counter({2: 3, 3: 2, 5: 1, 7: 1}) >>> get_factors(23) Counter({23: 1}) >>> get_factors(0) Traceback (most recent call last):
…
TypeError: number must be integer and greater than zero >>> get_factors(-1) Traceback (most recent call last):
…
TypeError: number must be integer and greater than zero >>> get_factors(1.5) Traceback (most recent call last):
…
TypeError: number must be integer and greater than zero
factor can be all numbers from 2 to number that we check if number % factor == 0 if it is equal to zero, we check again with number // factor else we increase factor by one
- maths.gcd_of_n_numbers.get_greatest_common_divisor(*numbers: int) int ¶
get gcd of n numbers: >>> get_greatest_common_divisor(18, 45) 9 >>> get_greatest_common_divisor(23, 37) 1 >>> get_greatest_common_divisor(2520, 8350) 10 >>> get_greatest_common_divisor(-10, 20) Traceback (most recent call last):
…
Exception: numbers must be integer and greater than zero >>> get_greatest_common_divisor(1.5, 2) Traceback (most recent call last):
…
Exception: numbers must be integer and greater than zero >>> get_greatest_common_divisor(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) 1 >>> get_greatest_common_divisor(“1”, 2, 3, 4, 5, 6, 7, 8, 9, 10) Traceback (most recent call last):
…
Exception: numbers must be integer and greater than zero