diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-29 15:57:05 +0100 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2024-09-29 15:57:05 +0100 |
| commit | f47a1ad4586b420621d86318e579d3a1f5742554 (patch) | |
| tree | 22334f83c118bfe0c0dff4894488e6285bca39bc | |
| parent | 2e3834c4b714159c7f247c53af25aff427533dde (diff) | |
| download | perlweeklychallenge-club-f47a1ad4586b420621d86318e579d3a1f5742554.tar.gz perlweeklychallenge-club-f47a1ad4586b420621d86318e579d3a1f5742554.tar.bz2 perlweeklychallenge-club-f47a1ad4586b420621d86318e579d3a1f5742554.zip | |
Add Python solution to challenge 164
| -rw-r--r-- | challenge-164/paulo-custodio/perl/ch-1.pl | 2 | ||||
| -rw-r--r-- | challenge-164/paulo-custodio/perl/ch-2.pl | 2 | ||||
| -rw-r--r-- | challenge-164/paulo-custodio/python/ch-1.py | 49 | ||||
| -rw-r--r-- | challenge-164/paulo-custodio/python/ch-2.py | 52 |
4 files changed, 103 insertions, 2 deletions
diff --git a/challenge-164/paulo-custodio/perl/ch-1.pl b/challenge-164/paulo-custodio/perl/ch-1.pl index 82ff5f5ded..6491052171 100644 --- a/challenge-164/paulo-custodio/perl/ch-1.pl +++ b/challenge-164/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 164 # diff --git a/challenge-164/paulo-custodio/perl/ch-2.pl b/challenge-164/paulo-custodio/perl/ch-2.pl index 1e48d07424..e92fc69a1b 100644 --- a/challenge-164/paulo-custodio/perl/ch-2.pl +++ b/challenge-164/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 164 # diff --git a/challenge-164/paulo-custodio/python/ch-1.py b/challenge-164/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..90c9d3b8e3 --- /dev/null +++ b/challenge-164/paulo-custodio/python/ch-1.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +# Challenge 164 +# +# Task 1: Prime Palindrome +# Submitted by: Mohammad S Anwar +# +# Write a script to find all prime numbers less than 1000, which are also +# palindromes in base 10. Palindromic numbers are numbers whose digits are the +# same in reverse. For example, 313 is a palindromic prime, but 337 is not, even +# though 733 (337 reversed) is also prime. + +import sys + +def is_prime(n): + if n <= 1: + return False + if n <= 3: + return True + if n % 2 == 0 or n % 3 == 0: + return False + i = 5 + while i * i <= n: + if n % i == 0 or n % (i + 2) == 0: + return False + i += 6 + return True + +def next_prime(n): + if n <= 1: + return 2 + p = n + while not is_prime(p := p + 1): + pass + return p + +def primes(n): + p = 2 + prime_list = [] + while p <= n: + prime_list.append(p) + p = next_prime(p) + return prime_list + +def palindrome_primes(n): + prime_list = primes(n) + return [p for p in prime_list if str(p) == str(p)[::-1]] + +print(", ".join(map(str, palindrome_primes(int(sys.argv[1]))))) diff --git a/challenge-164/paulo-custodio/python/ch-2.py b/challenge-164/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..a54f423217 --- /dev/null +++ b/challenge-164/paulo-custodio/python/ch-2.py @@ -0,0 +1,52 @@ +#!/usr/bin/env python3 + +# Challenge 164 +# +# Task 2: Happy Numbers +# Submitted by: Robert DiCicco +# +# Write a script to find the first 8 Happy Numbers in base 10. For more +# information, please check out Wikipedia. +# +# Starting with any positive integer, replace the number by the sum of the +# squares of its digits, and repeat the process until the number equals 1 (where +# it will stay), or it loops endlessly in a cycle which does not include 1. +# +# Those numbers for which this process end in 1 are happy numbers, while those +# numbers that do not end in 1 are unhappy numbers. +# Example +# +# 19 is Happy Number in base 10, as shown: +# +# 19 => 1^2 + 9^2 +# => 1 + 81 +# => 82 => 8^2 + 2^2 +# => 64 + 4 +# => 68 => 6^2 + 8^2 +# => 36 + 64 +# => 100 => 1^2 + 0^2 + 0^2 +# => 1 + 0 + 0 +# => 1 + +import sys +from typing import List + +def is_happy(n: int) -> bool: + seen = set() + while n != 1: + if n in seen: + return False + seen.add(n) + n = sum(int(digit) ** 2 for digit in str(n)) + return True + +def happy_numbers(n: int) -> List[int]: + happy = [] + i = 1 + while len(happy) < n: + if is_happy(i): + happy.append(i) + i += 1 + return happy + +print(", ".join(map(str, happy_numbers(int(sys.argv[1]))))) |
