From 19698b60b5f8bbc16989a16c47fe41fa435cede3 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 19:10:26 +0100 Subject: Add Python solution to challenge 154 --- challenge-154/paulo-custodio/perl/ch-1.pl | 2 +- challenge-154/paulo-custodio/perl/ch-2.pl | 4 +- challenge-154/paulo-custodio/python/ch-1.py | 25 +++++++++++++ challenge-154/paulo-custodio/python/ch-2.py | 57 +++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 challenge-154/paulo-custodio/python/ch-1.py create mode 100644 challenge-154/paulo-custodio/python/ch-2.py diff --git a/challenge-154/paulo-custodio/perl/ch-1.pl b/challenge-154/paulo-custodio/perl/ch-1.pl index 7176aa1fdf..1d84fcd558 100644 --- a/challenge-154/paulo-custodio/perl/ch-1.pl +++ b/challenge-154/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 154 # -# TASK #1 › Missing Permutation +# TASK #1 > Missing Permutation # Submitted by: Mohammad S Anwar # You are given possible permutations of the string 'PERL'. # diff --git a/challenge-154/paulo-custodio/perl/ch-2.pl b/challenge-154/paulo-custodio/perl/ch-2.pl index c70b72378a..0b68acc1fa 100644 --- a/challenge-154/paulo-custodio/perl/ch-2.pl +++ b/challenge-154/paulo-custodio/perl/ch-2.pl @@ -2,9 +2,9 @@ # Challenge 154 # -# TASK #2 › Padovan Prime +# TASK #2 > Padovan Prime # Submitted by: Mohammad S Anwar -# A Padovan Prime is a Padovan Number that’s also prime. +# A Padovan Prime is a Padovan Number that's also prime. # # In number theory, the Padovan sequence is the sequence of integers P(n) # defined by the initial values. diff --git a/challenge-154/paulo-custodio/python/ch-1.py b/challenge-154/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..7deb2cd296 --- /dev/null +++ b/challenge-154/paulo-custodio/python/ch-1.py @@ -0,0 +1,25 @@ +#!/usr/bin/env perl + +# Challenge 154 +# +# TASK #1 > Missing Permutation +# Submitted by: Mohammad S Anwar +# You are given possible permutations of the string 'PERL'. +# +# PELR, PREL, PERL, PRLE, PLER, PLRE, EPRL, EPLR, ERPL, +# ERLP, ELPR, ELRP, RPEL, RPLE, REPL, RELP, RLPE, RLEP, +# LPER, LPRE, LEPR, LRPE, LREP +# Write a script to find any permutations missing from the list. + +from itertools import permutations + +have = { + "PELR", "PREL", "PERL", "PRLE", "PLER", "PLRE", "EPRL", "EPLR", "ERPL", + "ERLP", "ELPR", "ELRP", "RPEL", "RPLE", "REPL", "RELP", "RLPE", "RLEP", + "LPER", "LPRE", "LEPR", "LRPE", "LREP" +} + +all_permutations = {''.join(p) for p in permutations("PERL")} +missing = sorted(set(all_permutations) - have) + +print(", ".join(missing)) diff --git a/challenge-154/paulo-custodio/python/ch-2.py b/challenge-154/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..91384eb9f6 --- /dev/null +++ b/challenge-154/paulo-custodio/python/ch-2.py @@ -0,0 +1,57 @@ +#!/usr/bin/env perl + +# Challenge 154 +# +# TASK #2 > Padovan Prime +# Submitted by: Mohammad S Anwar +# A Padovan Prime is a Padovan Number that's also prime. +# +# In number theory, the Padovan sequence is the sequence of integers P(n) +# defined by the initial values. +# +# P(0) = P(1) = P(2) = 1 +# and then followed by +# +# P(n) = P(n-2) + P(n-3) +# First few Padovan Numbers are as below: +# +# 1, 1, 1, 2, 2, 3, 4, 5, 7, 9, 12, 16, 21, 28, 37, ... +# Write a script to compute first 10 distinct Padovan Primes. +# +# Expected Output +# 2, 3, 5, 7, 37, 151, 3329, 23833, 13091204281, 3093215881333057 + +from functools import lru_cache +from sympy import isprime + +@lru_cache(None) +def padovan(n): + if n < 3: + return 1 + else: + return padovan(n - 2) + padovan(n - 3) + +def padovan_iter(): + n = 0 + while True: + n += 1 + yield padovan(n) + +def padovan_prime_iter(): + padovan_it = padovan_iter() + for got in padovan_it: + if isprime(got): + yield got + +def uniq_padovan_prime_it(): + got = set() + padovan_prime_it = padovan_prime_iter() + while True: + for value in padovan_prime_it: + if value not in got: + got.add(value) + yield value + +it = uniq_padovan_prime_it() +out = [next(it) for _ in range(10)] +print(", ".join(map(str, out))) -- cgit From 341287fb7780a3c5ee45ec0521ef99d11dbdbcce Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 19:23:13 +0100 Subject: Add Python solution to challenge 155 --- challenge-065/paulo-custodio/python/ch-2.py | 2 +- challenge-154/paulo-custodio/python/ch-1.py | 2 +- challenge-154/paulo-custodio/python/ch-2.py | 2 +- challenge-155/paulo-custodio/perl/ch-1.pl | 2 +- challenge-155/paulo-custodio/perl/ch-2.pl | 2 +- challenge-155/paulo-custodio/python/ch-1.py | 32 ++++++++++++++++ challenge-155/paulo-custodio/python/ch-2.py | 57 +++++++++++++++++++++++++++++ 7 files changed, 94 insertions(+), 5 deletions(-) create mode 100644 challenge-155/paulo-custodio/python/ch-1.py create mode 100644 challenge-155/paulo-custodio/python/ch-2.py diff --git a/challenge-065/paulo-custodio/python/ch-2.py b/challenge-065/paulo-custodio/python/ch-2.py index 702ed4d413..7d037bdc0d 100644 --- a/challenge-065/paulo-custodio/python/ch-2.py +++ b/challenge-065/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 065 # diff --git a/challenge-154/paulo-custodio/python/ch-1.py b/challenge-154/paulo-custodio/python/ch-1.py index 7deb2cd296..1b121593e6 100644 --- a/challenge-154/paulo-custodio/python/ch-1.py +++ b/challenge-154/paulo-custodio/python/ch-1.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 154 # diff --git a/challenge-154/paulo-custodio/python/ch-2.py b/challenge-154/paulo-custodio/python/ch-2.py index 91384eb9f6..0afb22504f 100644 --- a/challenge-154/paulo-custodio/python/ch-2.py +++ b/challenge-154/paulo-custodio/python/ch-2.py @@ -1,4 +1,4 @@ -#!/usr/bin/env perl +#!/usr/bin/env python3 # Challenge 154 # diff --git a/challenge-155/paulo-custodio/perl/ch-1.pl b/challenge-155/paulo-custodio/perl/ch-1.pl index ff00af24a0..80eb7c1565 100644 --- a/challenge-155/paulo-custodio/perl/ch-1.pl +++ b/challenge-155/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 155 # -# TASK #1 › Fortunate Numbers +# TASK #1 > Fortunate Numbers # Submitted by: Mohammad S Anwar # Write a script to produce first 8 Fortunate Numbers (unique and sorted). # diff --git a/challenge-155/paulo-custodio/perl/ch-2.pl b/challenge-155/paulo-custodio/perl/ch-2.pl index 4ff1516026..fbaa92c2ee 100644 --- a/challenge-155/paulo-custodio/perl/ch-2.pl +++ b/challenge-155/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 155 # -# TASK #2 › Pisano Period +# TASK #2 > Pisano Period # Submitted by: Mohammad S Anwar # Write a script to find the period of the 3rd Pisano Period. # diff --git a/challenge-155/paulo-custodio/python/ch-1.py b/challenge-155/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..e1cdaebf76 --- /dev/null +++ b/challenge-155/paulo-custodio/python/ch-1.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 + +# Challenge 155 +# +# TASK #1 > Fortunate Numbers +# Submitted by: Mohammad S Anwar +# Write a script to produce first 8 Fortunate Numbers (unique and sorted). +# +# According to Wikipedia +# +# A Fortunate number, named after Reo Fortune, is the smallest integer m > 1 +# such that, for a given positive integer n, pn# + m is a prime number, where +# the primorial pn# is the product of the first n prime numbers. +# +# Expected Output +# 3, 5, 7, 13, 17, 19, 23, 37 + +from math import prod +from sympy import isprime, nextprime + +fortunate = {} + +primes = [2] +while len(fortunate) < 8: + p = prod(primes) + m = 2 + while not isprime(p + m): + m += 1 + fortunate[m] = 1 + primes.append(nextprime(primes[-1])) + +print(", ".join(map(str, sorted(fortunate.keys())))) diff --git a/challenge-155/paulo-custodio/python/ch-2.py b/challenge-155/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..f5408b05db --- /dev/null +++ b/challenge-155/paulo-custodio/python/ch-2.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python3 + +# Challenge 155 +# +# TASK #2 > Pisano Period +# Submitted by: Mohammad S Anwar +# Write a script to find the period of the 3rd Pisano Period. +# +# In number theory, the nth Pisano period, written as p(n), is the period with +# which the sequence of Fibonacci numbers taken modulo n repeats. +# +# The Fibonacci numbers are the numbers in the integer sequence: +# +# 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, ... +# For any integer n, the sequence of Fibonacci numbers F(i) taken modulo n is +# periodic. The Pisano period, denoted p(n), is the value of the period of this +# sequence. For example, the sequence of Fibonacci numbers modulo 3 begins: +# +# 0, 1, 1, 2, 0, 2, 2, 1, +# 0, 1, 1, 2, 0, 2, 2, 1, +# 0, 1, 1, 2, 0, 2, 2, 1, ... +# This sequence has period 8, so p(3) = 8. + +from math import isqrt + +order = 3 + +def fibonacci_series(n): + fibs = [0, 1] + for i in range(2, n): + fibs.append(fibs[-1] + fibs[-2]) + return fibs + +def is_period(period, n): + base = n[0:period] + ord1 = n[period:2*period] + ord2 = n[2*period:3*period] + + if base != ord1: + return 0 + if base != ord2: + return 0 + return 1 + +def find_period(n): + period = 1 + while True: + if 3 * period > len(n): + return 0 + if is_period(period, n): + return period + period += 1 + return 0 + +fibs = fibonacci_series(100) +fibs_mod = [fib % order for fib in fibs] +print(find_period(fibs_mod)) -- cgit From 1e40fcc88e800e74dc8bc1291020a614b362cb61 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 19:38:42 +0100 Subject: Add Python solution to challenge 156 --- challenge-156/paulo-custodio/perl/ch-2.pl | 2 +- challenge-156/paulo-custodio/python/ch-1.py | 35 +++++++++++++++++ challenge-156/paulo-custodio/python/ch-2.py | 59 +++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 challenge-156/paulo-custodio/python/ch-1.py create mode 100644 challenge-156/paulo-custodio/python/ch-2.py diff --git a/challenge-156/paulo-custodio/perl/ch-2.pl b/challenge-156/paulo-custodio/perl/ch-2.pl index d16e291ea5..d77b690bbf 100644 --- a/challenge-156/paulo-custodio/perl/ch-2.pl +++ b/challenge-156/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 156 # -# TASK #2 › Weird Number +# TASK #2 > Weird Number # Submitted by: Mohammad S Anwar # You are given number, $n > 0. # diff --git a/challenge-156/paulo-custodio/python/ch-1.py b/challenge-156/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..dd16bf4390 --- /dev/null +++ b/challenge-156/paulo-custodio/python/ch-1.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 + +# Challenge 156 +# +# TASK #1 > Pernicious Numbers +# Submitted by: Mohammad S Anwar +# Write a script to permute first 10 Pernicious Numbers. +# +# A pernicious number is a positive integer which has prime number of ones in +# its binary representation. +# +# The first pernicious number is 3 since binary representation of 3 = (11) +# and 1 + 1 = 2, which is a prime. +# +# Expected Output +# 3, 5, 6, 7, 9, 10, 11, 12, 13, 14 + +from sympy import isprime + +def is_pernicious(n): + bin_n = bin(n)[2:] # Convert to binary and remove the '0b' prefix + ones = bin_n.count('1') + is_pernicious = isprime(ones) + return is_pernicious + +def first_pernicious(num): + out = [] + n = 0 + while len(out) < num: + if is_pernicious(n): + out.append(n) + n += 1 + return out + +print(", ".join(map(str, first_pernicious(10)))) diff --git a/challenge-156/paulo-custodio/python/ch-2.py b/challenge-156/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..b198001956 --- /dev/null +++ b/challenge-156/paulo-custodio/python/ch-2.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +# Challenge 156 +# +# TASK #2 > Weird Number +# Submitted by: Mohammad S Anwar +# You are given number, $n > 0. +# +# Write a script to find out if the given number is a Weird Number. +# +# According to Wikipedia, it is defined as: +# +# The sum of the proper divisors (divisors including 1 but not itself) of the +# number is greater than the number, but no subset of those divisors sums to +# the number itself. +# +# Example 1: +# Input: $n = 12 +# Output: 0 +# +# Since the proper divisors of 12 are 1, 2, 3, 4, and 6, which sum to 16; but +# 2 + 4 + 6 = 12. +# Example 2: +# Input: $n = 70 +# Output: 1 +# +# As the proper divisors of 70 are 1, 2, 5, 7, 10, 14, and 35; these sum to +# 74, but no subset of these sums to 70. + +import sys +from itertools import combinations +from math import isqrt + +def divisors(n): + divs = set() + for i in range(1, isqrt(n) + 1): + if n % i == 0: + divs.add(i) + divs.add(n // i) + return divs + +def proper_divisors(n): + return [d for d in divisors(n) if d != n] + +def check_no_subset_sums_n(n, div): + for k in range(1, len(div) + 1): + for combin in combinations(div, k): + if sum(combin) == n: + return False + return True + +def is_weird(n): + div = proper_divisors(n) + if sum(div) <= n: + return False + return check_no_subset_sums_n(n, div) + +n = int(sys.argv[1]) +print(1 if is_weird(n) else 0) -- cgit From bcbb8e543ac9ad74cf70fb749a5f7f917e78abb2 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 19:57:44 +0100 Subject: Add Python solution to challenge 157 --- challenge-157/paulo-custodio/perl/ch-1.pl | 2 +- challenge-157/paulo-custodio/perl/ch-2.pl | 2 +- challenge-157/paulo-custodio/python/ch-1.py | 46 +++++++++++++++++++++++++ challenge-157/paulo-custodio/python/ch-2.py | 53 +++++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 challenge-157/paulo-custodio/python/ch-1.py create mode 100644 challenge-157/paulo-custodio/python/ch-2.py diff --git a/challenge-157/paulo-custodio/perl/ch-1.pl b/challenge-157/paulo-custodio/perl/ch-1.pl index b6b2cdd4e6..36199d209e 100644 --- a/challenge-157/paulo-custodio/perl/ch-1.pl +++ b/challenge-157/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 157 # -# TASK #1 › Pythagorean Means +# TASK #1 > Pythagorean Means # Submitted by: Mohammad S Anwar # You are given a set of integers. # diff --git a/challenge-157/paulo-custodio/perl/ch-2.pl b/challenge-157/paulo-custodio/perl/ch-2.pl index 2c298e11a7..48af9d0de7 100644 --- a/challenge-157/paulo-custodio/perl/ch-2.pl +++ b/challenge-157/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 157 # -# TASK #2 › Brazilian Number +# TASK #2 > Brazilian Number # Submitted by: Mohammad S Anwar # You are given a number $n > 3. # diff --git a/challenge-157/paulo-custodio/python/ch-1.py b/challenge-157/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..726edff089 --- /dev/null +++ b/challenge-157/paulo-custodio/python/ch-1.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 + +# Challenge 157 +# +# TASK #1 > Pythagorean Means +# Submitted by: Mohammad S Anwar +# You are given a set of integers. +# +# Write a script to compute all three Pythagorean Means i.e Arithmetic Mean, +# Geometric Mean and Harmonic Mean of the given set of integers. Please refer +# to wikipedia page for more informations. +# +# Example 1: +# Input: @n = (1,3,5,6,9) +# Output: AM = 4.8, GM = 3.8, HM = 2.8 +# CORRECTION [2022-03-21 16:35] GM = 3.9 (before) +# +# Example 2: +# Input: @n = (2,4,6,8,10) +# Output: AM = 6.0, GM = 5.2, HM = 4.4 +# Example 3: +# Input: @n = (1,2,3,4,5) +# Output: AM = 3.0, GM = 2.6, HM = 2.2 + +import sys +import numpy as np + +def am(n): + return np.sum(n) / len(n) + +def gm(n): + base = abs(np.prod(n)) + exp = 1 / len(n) + return base ** exp + +def hm(n): + invn = [1/x for x in n] + return len(invn) / np.sum(invn) + +def f(n): + return f"{n:.1f}" + +n = list(map(float, sys.argv[1:])) +if not n: + raise ValueError("Usage: ch-1.py n...") +print(f"AM = {f(am(n))}, GM = {f(gm(n))}, HM = {f(hm(n))}") diff --git a/challenge-157/paulo-custodio/python/ch-2.py b/challenge-157/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..9da6f097cb --- /dev/null +++ b/challenge-157/paulo-custodio/python/ch-2.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python3 + +# Challenge 157 +# +# TASK #2 > Brazilian Number +# Submitted by: Mohammad S Anwar +# You are given a number $n > 3. +# +# Write a script to find out if the given number is a Brazilian Number. +# +# A positive integer number N has at least one natural number B where +# 1 < B < N-1 where the representation of N in base B has same digits. +# +# +# Example 1: +# Input: $n = 7 +# Output: 1 +# +# Since 7 in base 2 is 111. +# Example 2: +# Input: $n = 6 +# Output: 0 +# +# Since 6 in base 2 is 110, +# 6 in base 3 is 20 and +# 6 in base 4 is 12. +# Example 3: +# Input: $n = 8 +# Output: 1 +# +# Since 8 in base 3 is 22. + +import sys +from math import log + +def cnv(n, base): + if n == 0: + return '0' + digits = [] + while n: + digits.append(int(n % base)) + n //= base + return ''.join(str(x) for x in digits[::-1]) + +def is_brazilian(n): + for b in range(2, n - 1): + cnv_value = cnv(n, b) + if all(c == cnv_value[0] for c in cnv_value): + return True + return False + +n = int(sys.argv[1]) +print(1 if is_brazilian(n) else 0) -- cgit From 45ae6460d987618b7ca6016b89f7722c848893fa Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 21:03:54 +0100 Subject: Add Python solution to challenge 158 --- challenge-158/paulo-custodio/perl/ch-1.pl | 2 +- challenge-158/paulo-custodio/perl/ch-2.pl | 2 +- challenge-158/paulo-custodio/python/ch-1.py | 27 +++++++++++++++++++++++++ challenge-158/paulo-custodio/python/ch-2.py | 31 +++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 challenge-158/paulo-custodio/python/ch-1.py create mode 100644 challenge-158/paulo-custodio/python/ch-2.py diff --git a/challenge-158/paulo-custodio/perl/ch-1.pl b/challenge-158/paulo-custodio/perl/ch-1.pl index 1c23f8c0ff..c71f3fa9d0 100644 --- a/challenge-158/paulo-custodio/perl/ch-1.pl +++ b/challenge-158/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 158 # -# TASK #1 › Additive Primes +# TASK #1 > Additive Primes # Submitted by: Mohammad S Anwar # Write a script to find out all Additive Primes <= 100. # diff --git a/challenge-158/paulo-custodio/perl/ch-2.pl b/challenge-158/paulo-custodio/perl/ch-2.pl index 8f28f63531..9d5cb05d83 100644 --- a/challenge-158/paulo-custodio/perl/ch-2.pl +++ b/challenge-158/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 158 # -# TASK #2 › First Series Cuban Primes +# TASK #2 > First Series Cuban Primes # Submitted by: Mohammad S Anwar # Write a script to compute first series Cuban Primes <= 1000. Please refer # wikipedia page for more informations. diff --git a/challenge-158/paulo-custodio/python/ch-1.py b/challenge-158/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..ad3b7825eb --- /dev/null +++ b/challenge-158/paulo-custodio/python/ch-1.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +# Challenge 158 +# +# TASK #1 > Additive Primes +# Submitted by: Mohammad S Anwar +# Write a script to find out all Additive Primes <= 100. +# +# Additive primes are prime numbers for which the sum of their decimal digits +# are also primes. +# +# +# Output +# 2, 3, 5, 7, 11, 23, 29, 41, 43, 47, 61, 67, 83, 89 + +from sympy import isprime, nextprime + +def additive_primes(limit): + out = [] + prime = 2 + while prime < limit: + if isprime(sum(int(digit) for digit in str(prime))): + out.append(prime) + prime = nextprime(prime) + return out + +print(", ".join(map(str, additive_primes(100)))) diff --git a/challenge-158/paulo-custodio/python/ch-2.py b/challenge-158/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..1f7220d82b --- /dev/null +++ b/challenge-158/paulo-custodio/python/ch-2.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python3 + +# Challenge 158 +# +# TASK #2 > First Series Cuban Primes +# Submitted by: Mohammad S Anwar +# Write a script to compute first series Cuban Primes <= 1000. Please refer +# wikipedia page for more informations. +# +# Output +# 7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919. +# +# p=(x^3-y^3)/(x-y), x=y+1, y>0 +# <=> p=3y^2+3y+1, y>0 + +from sympy import isprime + +def cuban_primes(limit): + out = [] + y = 1 + p = 0 + while True: + p = 3 * y * y + 3 * y + 1 + if isprime(p): + out.append(p) + if p >= limit: + break + y += 1 + return out + +print(", ".join(map(str, cuban_primes(1000)))) -- cgit From 502be42efd777d4125a2dff0fbc73d3b10af3db4 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 21:10:20 +0100 Subject: Add Python solution to challenge 159 --- challenge-159/paulo-custodio/perl/ch-1.pl | 2 +- challenge-159/paulo-custodio/perl/ch-2.pl | 2 +- challenge-159/paulo-custodio/python/ch-1.py | 38 +++++++++++++++++++++++++++++ challenge-159/paulo-custodio/python/ch-2.py | 26 ++++++++++++++++++++ 4 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 challenge-159/paulo-custodio/python/ch-1.py create mode 100644 challenge-159/paulo-custodio/python/ch-2.py diff --git a/challenge-159/paulo-custodio/perl/ch-1.pl b/challenge-159/paulo-custodio/perl/ch-1.pl index de8cf8b75c..4497c797be 100644 --- a/challenge-159/paulo-custodio/perl/ch-1.pl +++ b/challenge-159/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 159 # -# TASK #1 › Farey Sequence +# TASK #1 > Farey Sequence # Submitted by: Mohammad S Anwar # You are given a positive number, $n. # diff --git a/challenge-159/paulo-custodio/perl/ch-2.pl b/challenge-159/paulo-custodio/perl/ch-2.pl index 0b8df98a0b..52a8efa892 100644 --- a/challenge-159/paulo-custodio/perl/ch-2.pl +++ b/challenge-159/paulo-custodio/perl/ch-2.pl @@ -2,7 +2,7 @@ # Challenge 159 # -# TASK #2 › Moebius Number +# TASK #2 > Moebius Number # Submitted by: Mohammad S Anwar # You are given a positive number $n. # diff --git a/challenge-159/paulo-custodio/python/ch-1.py b/challenge-159/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..70c306c5fa --- /dev/null +++ b/challenge-159/paulo-custodio/python/ch-1.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +# Challenge 159 +# +# TASK #1 > Farey Sequence +# Submitted by: Mohammad S Anwar +# You are given a positive number, $n. +# +# Write a script to compute Farey Sequence of the order $n. +# +# Example 1: +# Input: $n = 5 +# Output: 0/1, 1/5, 1/4, 1/3, 2/5, 1/2, 3/5, 2/3, 3/4, 4/5, 1/1. +# Example 2: +# Input: $n = 7 +# Output: 0/1, 1/7, 1/6, 1/5, 1/4, 2/7, 1/3, 2/5, 3/7, 1/2, 4/7, 3/5, 2/3, 5/7, 3/4, 4/5, 5/6, 6/7, 1/1. +# Example 3: +# Input: $n = 4 +# Output: 0/1, 1/4, 1/3, 1/2, 2/3, 3/4, 1/1. + +from math import gcd +import sys + +def farey_sequence(n): + seq = [(0, 1), (1, 1)] # first and last terms + + for i in range(1, n + 1): + for j in range(i + 1, n + 1): + if gcd(i, j) == 1: + seq.append((i, j)) + + seq.sort(key=lambda x: x[0] / x[1]) + seq = [f"{num[0]}/{num[1]}" for num in seq] + + return seq + +n = int(sys.argv[1]) +print(", ".join(farey_sequence(n))) diff --git a/challenge-159/paulo-custodio/python/ch-2.py b/challenge-159/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..2ca571f2a1 --- /dev/null +++ b/challenge-159/paulo-custodio/python/ch-2.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python3 + +# Challenge 159 +# +# TASK #2 > Moebius Number +# Submitted by: Mohammad S Anwar +# You are given a positive number $n. +# +# Write a script to generate the Moebius Number for the given number. +# Please refer to wikipedia page for more informations. +# +# Example 1: +# Input: $n = 5 +# Output: -1 +# Example 2: +# Input: $n = 10 +# Output: 1 +# Example 3: +# Input: $n = 20 +# Output: 0 + +from sympy.functions.combinatorial.numbers import mobius +import sys + +n = int(sys.argv[1]) +print(mobius(n)) -- cgit From 85dfd261a678406d6d22eb45168fd3fb4d93c3a3 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sat, 28 Sep 2024 21:18:34 +0100 Subject: Add Python solution to challenge 160 --- challenge-160/paulo-custodio/perl/ch-1.pl | 4 +-- challenge-160/paulo-custodio/perl/ch-2.pl | 6 ++-- challenge-160/paulo-custodio/python/ch-1.py | 44 +++++++++++++++++++++++++++++ challenge-160/paulo-custodio/python/ch-2.py | 40 ++++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 challenge-160/paulo-custodio/python/ch-1.py create mode 100644 challenge-160/paulo-custodio/python/ch-2.py diff --git a/challenge-160/paulo-custodio/perl/ch-1.pl b/challenge-160/paulo-custodio/perl/ch-1.pl index 958713a46a..db13fb71c3 100644 --- a/challenge-160/paulo-custodio/perl/ch-1.pl +++ b/challenge-160/paulo-custodio/perl/ch-1.pl @@ -2,12 +2,12 @@ # Challenge 160 # -# TASK #1 › Four Is Magic +# TASK #1 > Four Is Magic # Submitted by: Mohammad S Anwar # You are given a positive number, $n < 10. # # Write a script to generate english text sequence starting with the English -# cardinal representation of the given number, the word ‘is’ and then the +# cardinal representation of the given number, the word 'is' and then the # English cardinal representation of the count of characters that made up the # first word, followed by a comma. Continue until you reach four. # diff --git a/challenge-160/paulo-custodio/perl/ch-2.pl b/challenge-160/paulo-custodio/perl/ch-2.pl index 9036422a55..e72e3c27f5 100644 --- a/challenge-160/paulo-custodio/perl/ch-2.pl +++ b/challenge-160/paulo-custodio/perl/ch-2.pl @@ -2,15 +2,15 @@ # Challenge 160 # -# TASK #2 › Equilibrium Index +# TASK #2 > Equilibrium Index # Submitted by: Mohammad S Anwar # You are give an array of integers, @n. # # Write a script to find out the Equilibrium Index of the given array, if found. # # For an array A consisting n elements, index i is an equilibrium index if the -# sum of elements of subarray A[0…i-1] is equal to the sum of elements of -# subarray A[i+1…n-1]. +# sum of elements of subarray A[0...i-1] is equal to the sum of elements of +# subarray A[i+1...n-1]. # # # Example 1: diff --git a/challenge-160/paulo-custodio/python/ch-1.py b/challenge-160/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..1c0534868e --- /dev/null +++ b/challenge-160/paulo-custodio/python/ch-1.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +# Challenge 160 +# +# TASK #1 > Four Is Magic +# Submitted by: Mohammad S Anwar +# You are given a positive number, $n < 10. +# +# Write a script to generate english text sequence starting with the English +# cardinal representation of the given number, the word 'is' and then the +# English cardinal representation of the count of characters that made up the +# first word, followed by a comma. Continue until you reach four. +# +# +# Example 1: +# Input: $n = 5 +# Output: Five is four, four is magic. +# +# Example 2: +# Input: $n = 7 +# Output: Seven is five, five is four, four is magic. +# +# Example 3: +# Input: $n = 6 +# Output: Six is three, three is five, five is four, four is magic. + +import sys +from num2words import num2words + +def sequence(n=1): + out = [] + while n != 4: + num_en = num2words(n) + length = len(num_en) + len_en = num2words(length) + + out.append(f"{num_en} is {len_en}") + n = length + + out.append("four is magic.") + out[0] = out[0].capitalize() + return ", ".join(out) + +print(sequence(int(sys.argv[1]))) diff --git a/challenge-160/paulo-custodio/python/ch-2.py b/challenge-160/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..18b3916dc1 --- /dev/null +++ b/challenge-160/paulo-custodio/python/ch-2.py @@ -0,0 +1,40 @@ +#!/usr/bin/env python3 + +# Challenge 160 +# +# TASK #2 > Equilibrium Index +# Submitted by: Mohammad S Anwar +# You are give an array of integers, @n. +# +# Write a script to find out the Equilibrium Index of the given array, if found. +# +# For an array A consisting n elements, index i is an equilibrium index if the +# sum of elements of subarray A[0...i-1] is equal to the sum of elements of +# subarray A[i+1...n-1]. +# +# +# Example 1: +# Input: @n = (1, 3, 5, 7, 9) +# Output: 3 +# +# Example 2: +# Input: @n = (1, 2, 3, 4, 5) +# Output: -1 as no Equilibrium Index found. +# +# Example 3: +# Input: @n = (2, 4, 2) +# Output: 1 + +import sys + +def equilibrium_index(n): + for i in range(1, len(n) - 1): + left = sum(n[0:i]) + right = sum(n[i + 1:]) + if left == right: + return i + if left > right: + return -1 + return -1 + +print(equilibrium_index(list(map(int, sys.argv[1:])))) -- cgit From 3bfbde53c142da8b0a3176380aa87a7534658fca Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 29 Sep 2024 15:18:51 +0100 Subject: Add Python solution to challenge 162 --- challenge-001/paulo-custodio/go.pl | 5 +- challenge-162/paulo-custodio/python/ch-1.py | 28 ++++++++ challenge-162/paulo-custodio/python/ch-2.py | 105 ++++++++++++++++++++++++++++ 3 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 challenge-162/paulo-custodio/python/ch-1.py create mode 100644 challenge-162/paulo-custodio/python/ch-2.py diff --git a/challenge-001/paulo-custodio/go.pl b/challenge-001/paulo-custodio/go.pl index 3904778b45..b41860b43a 100644 --- a/challenge-001/paulo-custodio/go.pl +++ b/challenge-001/paulo-custodio/go.pl @@ -9,8 +9,9 @@ my $nr = sprintf("%03d", $ARGV[0]); path("challenge-$nr/paulo-custodio")->mkpath; #for my $dir (qw(ada awk basic bc brainfuck c cpp d forth fortran lua pascal perl python t)) { -# path("challenge-$nr/paulo-custodio/$dir")->mkpath; -#} +for my $dir (qw( perl python t )) { + path("challenge-$nr/paulo-custodio/$dir")->mkpath; +} path("challenge-$nr/paulo-custodio/README")->spew("Solution by Paulo Custodio\n"); if (! -f "challenge-$nr/paulo-custodio/Makefile") { diff --git a/challenge-162/paulo-custodio/python/ch-1.py b/challenge-162/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..1b31447b25 --- /dev/null +++ b/challenge-162/paulo-custodio/python/ch-1.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 + +# Challenge 162 +# +# Task 1: ISBN-13 +# Submitted by: Mohammad S Anwar +# Write a script to generate the check digit of given ISBN-13 code. Please +# refer wikipedia for more information. +# +# Example +# ISBN-13 check digit for '978-0-306-40615-7' is 7. + +import sys + +def isbn13_check_digit(isbn): + m = 1 + sum_ = 0 + isbn = isbn.replace('-', '') + for i in range(12): + digit = int(isbn[i]) + sum_ += m * digit + m = 3 if m==1 else 1 + digit = sum_ % 10 + if digit != 0: + digit = 10-digit + return digit + +print(isbn13_check_digit(sys.argv[1])) diff --git a/challenge-162/paulo-custodio/python/ch-2.py b/challenge-162/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..aad3aadf83 --- /dev/null +++ b/challenge-162/paulo-custodio/python/ch-2.py @@ -0,0 +1,105 @@ +#!/usr/bin/env python3 + +# Challenge 162 +# +# Task 2: Wheatstone-Playfair +# Submitted by: Roger Bell_West +# Implement encryption and decryption using the Wheatstone-Playfair cipher. +# +# Examples: +# (These combine I and J, and use X as padding.) +# +# encrypt("playfair example", "hide the gold in the tree stump") +# = "bmodzbxdnabekudmuixmmouvif" +# +# decrypt("perl and raku", "siderwrdulfipaarkcrw") = "thewexeklychallengex" + +class WPcipher: + def __init__(self, key_phrase): + self.key = [[] for _ in range(5)] + self.letters = {} + self.make_key(key_phrase) + + def make_key(self, key_phrase): + row, col = 0, 0 + for c in (key_phrase.lower() + ''.join(chr(i) for i in range(ord('a'), ord('z') + 1))): + if not c.isalpha() or c not in 'abcdefghijklmnopqrstuvwxyz': + continue + c = 'i' if c == 'j' else c + if c in self.letters: + continue + + self.key[row].append(c) + self.letters[c] = (row, col) + + col += 1 + if col >= 5: + row += 1 + col = 0 + if row >= 5: + break + + def peek(self, row, col): + return self.key[(row + 5) % 5][(col + 5) % 5] + + def encrypt_pair(self, a, b, direction): + for char in (a, b): + if char not in 'abcdefghiklmnopqrstuvwxyz': + raise ValueError(f"Invalid character: {char}") + if a == b: + raise ValueError("Characters must not be the same") + + row1, col1 = self.letters[a] + row2, col2 = self.letters[b] + + if row1 == row2: + return (self.peek(row1, col1 + direction), + self.peek(row2, col2 + direction)) + elif col1 == col2: + return (self.peek(row1 + direction, col1), + self.peek(row2 + direction, col2)) + else: + return (self.peek(row1, col2), + self.peek(row2, col1)) + + def encrypt_string(self, text, direction): + text = text.lower() + out = "" + while text: + if len(text) == 1: + text += "x" + if text[0] == text[1:2]: # repeated first character + a = text[0] + b = "x" + text = text[1:] + else: + a = text[0] + b = text[1] + text = text[2:] + + x, y = self.encrypt_pair(a, b, direction) + out += x + y + return out + + def encrypt(self, text): + text = ''.join(filter(str.isalpha, text)).lower().replace('j', 'i') + return self.encrypt_string(text, 1) + + def decrypt(self, code): + return self.encrypt_string(code, -1) + + +import sys + +if len(sys.argv) != 4: + raise ValueError("Usage: ch-2.py -e|-d key text") + +op, key_phrase, text = sys.argv[1], sys.argv[2], sys.argv[3] + +wp = WPcipher(key_phrase) +if op == "-e": + print(wp.encrypt(text)) +elif op == "-d": + print(wp.decrypt(text)) +else: + raise ValueError("Usage: ch-2.py -e|-d key text") -- cgit From 2e3834c4b714159c7f247c53af25aff427533dde Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 29 Sep 2024 15:50:36 +0100 Subject: Add Python solution to challenge 163 --- challenge-163/paulo-custodio/perl/ch-1.pl | 2 +- challenge-163/paulo-custodio/perl/ch-2.pl | 2 +- challenge-163/paulo-custodio/python/ch-1.py | 33 +++++++++++++++++++ challenge-163/paulo-custodio/python/ch-2.py | 51 +++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 challenge-163/paulo-custodio/python/ch-1.py create mode 100644 challenge-163/paulo-custodio/python/ch-2.py diff --git a/challenge-163/paulo-custodio/perl/ch-1.pl b/challenge-163/paulo-custodio/perl/ch-1.pl index 4e30d9c4a0..71603848ac 100644 --- a/challenge-163/paulo-custodio/perl/ch-1.pl +++ b/challenge-163/paulo-custodio/perl/ch-1.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 163 # diff --git a/challenge-163/paulo-custodio/perl/ch-2.pl b/challenge-163/paulo-custodio/perl/ch-2.pl index abaed6e0f4..918a8874be 100644 --- a/challenge-163/paulo-custodio/perl/ch-2.pl +++ b/challenge-163/paulo-custodio/perl/ch-2.pl @@ -1,4 +1,4 @@ -#!/usr/bin/perl +#!/usr/bin/env perl # Challenge 163 # diff --git a/challenge-163/paulo-custodio/python/ch-1.py b/challenge-163/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..67a949985e --- /dev/null +++ b/challenge-163/paulo-custodio/python/ch-1.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python3 + +# Challenge 163 +# +# Task 1: Sum Bitwise Operator +# Submitted by: Mohammad S Anwar +# +# You are given list positive numbers, @n. +# +# Write script to calculate the sum of bitwise & operator for all unique pairs. +# Example 1 +# +# Input: @n = (1, 2, 3) +# Output: 3 +# +# Since (1 & 2) + (2 & 3) + (1 & 3) => 0 + 2 + 1 => 3. +# +# Example 2 +# +# Input: @n = (2, 3, 4) +# Output: 2 +# +# Since (2 & 3) + (2 & 4) + (3 & 4) => 2 + 0 + 0 => 2. + +def sum_biwise_and(*n): + total_sum = 0 + for i in range(len(n) - 1): + for j in range(i + 1, len(n)): + total_sum += n[i] & n[j] + return total_sum + +import sys +print(sum_biwise_and(*map(int, sys.argv[1:]))) diff --git a/challenge-163/paulo-custodio/python/ch-2.py b/challenge-163/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..12f03bb6e6 --- /dev/null +++ b/challenge-163/paulo-custodio/python/ch-2.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 + +# Challenge 163 +# +# Task 2: Summations +# Submitted by: Mohammad S Anwar +# +# You are given a list of positive numbers, @n. +# +# Write a script to find out the summations as described below. +# Example 1 +# +# Input: @n = (1, 2, 3, 4, 5) +# Output: 42 +# +# 1 2 3 4 5 +# 2 5 9 14 +# 5 14 28 +# 14 42 +# 42 +# +# The nth Row starts with the second element of the (n-1)th row. +# The following element is sum of all elements except first element of previous +# row. +# You stop once you have just one element in the row. +# +# Example 2 +# +# Input: @n = (1, 3, 5, 7, 9) +# Output: 70 +# +# 1 3 5 7 9 +# 3 8 15 24 +# 8 23 47 +# 23 70 +# 70 + +import sys +from functools import reduce + +def summation(*n): + n = list(n) + while len(n) > 1: + n.pop(0) + m = [] + for i in range(len(n)): + m.append(sum(n[:i + 1])) + n = m + return n[0] + +print(summation(*map(int, sys.argv[1:]))) -- cgit From f47a1ad4586b420621d86318e579d3a1f5742554 Mon Sep 17 00:00:00 2001 From: Paulo Custodio Date: Sun, 29 Sep 2024 15:57:05 +0100 Subject: Add Python solution to challenge 164 --- challenge-164/paulo-custodio/perl/ch-1.pl | 2 +- challenge-164/paulo-custodio/perl/ch-2.pl | 2 +- challenge-164/paulo-custodio/python/ch-1.py | 49 +++++++++++++++++++++++++++ challenge-164/paulo-custodio/python/ch-2.py | 52 +++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 challenge-164/paulo-custodio/python/ch-1.py create mode 100644 challenge-164/paulo-custodio/python/ch-2.py 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]))))) -- cgit