Skip to content

gcd_of_n_numbers #8057

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Jan 10, 2023
Merged
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
83aa457
add maths/Gcd of N Numbers
balakhaniyan Dec 27, 2022
1dd1284
add maths/Gcd of N Numbers
balakhaniyan Dec 27, 2022
89b8336
add maths/Gcd of N Numbers
balakhaniyan Dec 27, 2022
3747c46
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 27, 2022
8996186
add maths/Gcd of N Numbers
balakhaniyan Dec 27, 2022
bc3ea36
Merge branch 'master' of https://github.com/balakhaniyan/Python
balakhaniyan Dec 27, 2022
c774f82
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 27, 2022
0200bd6
add maths/Gcd of N Numbers
balakhaniyan Dec 27, 2022
e684640
Merge branch 'master' of https://github.com/balakhaniyan/Python
balakhaniyan Dec 27, 2022
da603f6
add maths/Gcd of N Numbers
balakhaniyan Dec 27, 2022
95e67aa
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Dec 27, 2022
27f48bd
add maths/Gcd of N Numbers
balakhaniyan Dec 27, 2022
e02a30c
Merge branch 'master' of https://github.com/balakhaniyan/Python
balakhaniyan Dec 27, 2022
2f660fc
add maths/Gcd of N Numbers
balakhaniyan Dec 27, 2022
4099e95
more pythonic
balakhaniyan Jan 10, 2023
ec207a8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 10, 2023
8406035
more pythonic
balakhaniyan Jan 10, 2023
99af232
merged
balakhaniyan Jan 10, 2023
d5681d8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 10, 2023
db4f11b
merged
balakhaniyan Jan 10, 2023
8b321ba
more readable
balakhaniyan Jan 10, 2023
b7473dd
merged
balakhaniyan Jan 10, 2023
a230d7e
more readable
balakhaniyan Jan 10, 2023
d351516
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jan 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions maths/gcd_of_n_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

from collections import Counter
from functools import reduce


def get_factors(
Expand Down Expand Up @@ -87,23 +88,19 @@ def get_greatest_common_divisor(*numbers: int) -> int:

# we just need factors, not numbers itself
try:
data = [get_factors(number) for number in numbers]
same_factors, *factors = map(get_factors, numbers)
Copy link
Member

@cclauss cclauss Jan 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

except TypeError as e:
raise Exception("numbers must be integer and greater than zero") from e

same_factors: Counter = data[0]
for d in data[1:]:
same_factors = same_factors & d
for factor in factors:
same_factors &= factor
# get common factor between all
# `&` return common elements with smaller value (for Counter type)

# now, same_factors is something like {2: 2, 3: 4} that means 2 * 2 * 3 * 3 * 3 * 3
mult = 1
# power each factor and multiply
# for {2: 2, 3: 4}, it is [4, 81] and then 324
for m in [factor ** same_factors[factor] for factor in same_factors]:
mult *= m
return mult
return reduce(lambda x, y: x * y, (factor ** power for factor, power in same_factors.items()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to be hard to understand and maintain.



if __name__ == "__main__":
Expand Down