Weekly Challenge 393
Each week Mohammad S. Anwar sends out The Weekly Challenge, a chance for all of us to come up with solutions to two weekly tasks. My solutions are written in Python first, and then converted to Perl. Unless otherwise stated, Copilot (and other AI tools) have NOT been used to generate the solution. It's a great way for us all to practice some coding.
Task 1: Pythagoras Multiplied
You are given a positive integer n.
Find the number of all positive integer triplets (a, b, c) so that a² + b² = c² and a, b and c are integers <= n.
My solution
Since $a and $b are special variables in Perl, I call the values i, j and k. I start my solution by creating a function called is_valid_pythagoras which checks that k is an integer and it is less than or equal to n.
def is_valid_pythagoras(k: int | float, n: int) -> bool:
return k == int(k) and k <= n
The main function finds all combinations of i and j (between 1 and n), and checks that k (being the square root of i² + j²) matches the criteria. As i and j can be swapped, I count two at a time.
def pythagoras_multiplied(n: int) -> int:
return sum(
2
for i, j in combinations(range(1, 1 + n), 2)
if is_valid_pythagoras(math.sqrt(i**2 + j**2), n)
)
The Perl solution follows similar logic. As Algorithm::Combinatorics raises a warning if the length of the array is less than two, I handle the case if n is 1 manually.
use Algorithm::Combinatorics 'combinations';
sub main ($n) {
my $count = 0;
if ($n == 1) {
# Prevent Parameter k is greater than the size of data warning
say 0;
return;
}
# Generate all combinations
my $iter = combinations( [ 1 .. $n ], 2 );
# Loop through all combinations of 1 <= i < j <= n
while ( my $combination = $iter->next ) {
my ( $i, $j ) = @$combination;
# Check that 'k' is an integer and less than or equal to 'n'
my $k = sqrt( $i**2 + $j**2 );
if ( $k == int($k) and $k <= $n ) {
# We add two to count the opposite a and b values
$count += 2;
}
}
# Return the result
say $count;
}
Examples
$ ./ch-1.py 20
12
$ ./ch-1.py 7
2
$ ./ch-1.py 1
0
$ ./ch-1.py 15
8
$ ./ch-1.py 30
22
Task 2: Prime Step
You are given a string with English alphabetic characters only.
What is the absolute difference of the sum of the ASCII values of the characters in the string to the nearest prime number?
My solution
I start this challenge, by creating a function called is_prime. Given an integer it will return a boolean if the number is a prime.
def is_prime(n: int) -> bool:
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return n >= 2
In the main function, I start by checking that the input is valid.
def prime_step(word: str) -> int:
if not re.search(r"^[A-Za-z]+$", word):
raise ValueError("Input can only contain English letters")
I then calculate the sum of ASCII representation of the supplied string.
target = sum(ord(char) for char in word)
If target is a prime, I return 0. Otherwise, I have a loop called diff that increments by one and checks if the target-diff to target+diff is a prime. If it is, the code will return that function. If it isn't, the loop runs again after incrementing the diff value.
if is_prime(target):
return 0
diff = 1
while True:
if is_prime(target - diff) or is_prime(target + diff):
return diff
diff += 1
Examples
$ ./ch-2.py hello
9
$ ./ch-2.py football
2
$ ./ch-2.py a
0
$ ./ch-2.py challenge
2
$ ./ch-2.py perl
2
Top comments (0)