diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-01-14 14:15:23 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-14 14:15:23 +0000 |
| commit | e9758b7ad7eccd954c7b8a43832458bc22c04226 (patch) | |
| tree | 53d4fddef3e0e3285d57908a8cfdf64823df5f2a /challenge-147/sgreen/python/ch-2.py | |
| parent | e1c81b37bf462c531c42d7685b582ca669f658b9 (diff) | |
| parent | 263693e45cc6665e3c149cdcd4ba05875b8f9004 (diff) | |
| download | perlweeklychallenge-club-e9758b7ad7eccd954c7b8a43832458bc22c04226.tar.gz perlweeklychallenge-club-e9758b7ad7eccd954c7b8a43832458bc22c04226.tar.bz2 perlweeklychallenge-club-e9758b7ad7eccd954c7b8a43832458bc22c04226.zip | |
Merge pull request #5516 from simongreen-net/master
sgreen solutions to challenge 147
Diffstat (limited to 'challenge-147/sgreen/python/ch-2.py')
| -rwxr-xr-x | challenge-147/sgreen/python/ch-2.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-147/sgreen/python/ch-2.py b/challenge-147/sgreen/python/ch-2.py new file mode 100755 index 0000000000..c6424b9617 --- /dev/null +++ b/challenge-147/sgreen/python/ch-2.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python + +import math + + +def pentagon_number(n): + return int(n * (3 * n - 1) / 2) + + +def is_pentagon_number(x): + n = math.sqrt(24 * x + 1) + 1 + return int(n / 6) if n % 6 == 0 else None + + +def main(): + n1 = 2 + while True: + p1 = pentagon_number(n1) + for n2 in range(1, n1): + p2 = pentagon_number(n2) + + s1 = is_pentagon_number(p1 + p2) + s2 = is_pentagon_number(p1 - p2) + + if s1 is not None and s2 is not None: + print(f'P({n1}) + P({n2}) = {p1} + {p2} = { p1 + p2 } = P({s1})') + print(f'P({n1}) - P({n2}) = {p1} - {p2} = { p1 - p2 } = P({s2})') + return + n1 += 1 + + +main() |
