diff options
| author | Lubos Kolouch <lubos@kolouch.net> | 2023-07-08 15:43:27 +0200 |
|---|---|---|
| committer | Lubos Kolouch <lubos@kolouch.net> | 2023-07-08 15:43:27 +0200 |
| commit | d3821609694b5df45981b134d2bc8671743247d7 (patch) | |
| tree | 3112942127b45d1b01c1225e7a5a8a68c280c874 | |
| parent | 6d3108f8325d3434058b666ac0a893a5c62c6d8a (diff) | |
| download | perlweeklychallenge-club-d3821609694b5df45981b134d2bc8671743247d7.tar.gz perlweeklychallenge-club-d3821609694b5df45981b134d2bc8671743247d7.tar.bz2 perlweeklychallenge-club-d3821609694b5df45981b134d2bc8671743247d7.zip | |
feat(challenge-147/lubos-kolouch/perl,python/): Challenge 147 LK Perl Python
| -rw-r--r-- | challenge-147/lubos-kolouch/python/ch-1.py | 20 | ||||
| -rw-r--r-- | challenge-147/lubos-kolouch/python/ch-2.py | 26 |
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-147/lubos-kolouch/python/ch-1.py b/challenge-147/lubos-kolouch/python/ch-1.py new file mode 100644 index 0000000000..49e8c707d2 --- /dev/null +++ b/challenge-147/lubos-kolouch/python/ch-1.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +from sympy import isprime + + +def left_truncatable_primes(n): + primes = [] + queue = list(map(str, range(1, 10))) # we start from single-digit primes + while len(primes) < n: + num_str = queue.pop(0) + if isprime(int(num_str)): + primes.append(int(num_str)) + # adding digits to the end to try to form the next primes + for i in range(10): + queue.append(num_str + str(i)) + return primes[:n] + + +print(left_truncatable_primes(20)) diff --git a/challenge-147/lubos-kolouch/python/ch-2.py b/challenge-147/lubos-kolouch/python/ch-2.py new file mode 100644 index 0000000000..bd1577bd45 --- /dev/null +++ b/challenge-147/lubos-kolouch/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import math + + +def is_pentagonal(num): + return (math.sqrt(24 * num + 1) + 1) % 6 == 0 + + +def pentagonal_numbers(): + index = 1 + pentagonal_nums = [] + while True: + pentagonal_num = index * (3 * index - 1) // 2 + pentagonal_nums.append(pentagonal_num) + for j in range(index - 1, 0, -1): + if is_pentagonal(pentagonal_num - pentagonal_nums[j - 1]) and is_pentagonal( + pentagonal_num + pentagonal_nums[j - 1] + ): + return pentagonal_nums[j - 1], pentagonal_num + index += 1 + + +pair = pentagonal_numbers() +print(f"Pair of Pentagon Numbers: {pair[0]}, {pair[1]}") |
