diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-10-15 22:08:03 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-10-15 22:08:03 +0100 |
| commit | c5a68bd82df0591e7b379cdbf6b124bcf13d8bcc (patch) | |
| tree | 5aad4d045c1898eed90294f042a4cf23cf5ef7bf | |
| parent | 967a0f849325e8465ce02b272e355a66ba28a6fb (diff) | |
| parent | 439c7b5dba619933162da724ad2a6e84aa3365dd (diff) | |
| download | perlweeklychallenge-club-c5a68bd82df0591e7b379cdbf6b124bcf13d8bcc.tar.gz perlweeklychallenge-club-c5a68bd82df0591e7b379cdbf6b124bcf13d8bcc.tar.bz2 perlweeklychallenge-club-c5a68bd82df0591e7b379cdbf6b124bcf13d8bcc.zip | |
Merge pull request #2534 from LubosKolouch/master
Challenge 082 Task 1 solution Python LK
| -rw-r--r-- | challenge-082/lubos-kolouch/python/ch-1.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/challenge-082/lubos-kolouch/python/ch-1.py b/challenge-082/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..aa3e52865b --- /dev/null +++ b/challenge-082/lubos-kolouch/python/ch-1.py @@ -0,0 +1,45 @@ +#!/bin/env python +""" +#=============================================================================== +# +# FILE: ch-1.py +# +# USAGE: ./ch-1.py +# +# DESCRIPTION: Perl Weekly Challenge 082 +# http://www.perlweeklychallenge.org +# Task 1 - Common Factors +# +# AUTHOR: Lubos Kolouch +# CREATED: 10/15/2020 07:25:23 PM +#=============================================================================== +""" +import math + + +def get_common_factors(numbers): + + # 1 is always factor + common_factors = [1] + + # it's enough to to go sqrt of one of the numbers + for i in range(2, int(math.sqrt(numbers['first'])+1)): + if numbers['first'] % i != 0: + continue + + # add to solution if it is factor of the second number + if numbers['second'] % i == 0: + common_factors.append(i) + + # the same for the "other factor" + other_factor = numbers['first'] // i + if numbers['second'] % other_factor == 0: + common_factors.append(other_factor) + + common_factors.sort(key=int) + return common_factors + + +assert get_common_factors({'first': 12, 'second': 18}) == [1, 2, 3, 6] +assert get_common_factors({'first': 18, 'second': 23}) == [1] +assert get_common_factors({'first': 1, 'second': 1}) == [1] |
