From d3821609694b5df45981b134d2bc8671743247d7 Mon Sep 17 00:00:00 2001 From: Lubos Kolouch Date: Sat, 8 Jul 2023 15:43:27 +0200 Subject: feat(challenge-147/lubos-kolouch/perl,python/): Challenge 147 LK Perl Python --- challenge-147/lubos-kolouch/python/ch-1.py | 20 ++++++++++++++++++++ challenge-147/lubos-kolouch/python/ch-2.py | 26 ++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 challenge-147/lubos-kolouch/python/ch-1.py create mode 100644 challenge-147/lubos-kolouch/python/ch-2.py 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]}") -- cgit