The Weekly Challenge 393

Pythagoras Multiplied & Prime Step

Solutions on GitHub

Task 1: Pythagoras Multiplied

You are given a positive integer $n. Find the number of all positive integer triplets (a, b, c) such that a^2 + b^2 = c^2 and a, b, c <= n.

Example Output

Input: $n = 20 Output: 12 Input: $n = 7 Output: 2

Logic

For each positive integer a and b up to n, compute c^2 = a^2 + b^2. Take the integer square root c = isqrt(c^2) and check whether c <= n and c * c == c^2. Because order matters ((3,4,5) vs (4,3,5)), we iterate over all pairs (a, b) independently.

Perl Solution

ch-1.pl

=head1 NAME ch-1.pl - Pythagoras Multiplied =head1 DESCRIPTION You are given a positive integer $n. Find the number of all positive integer triplets (a, b, c) so that a^2 + b^2 = c^2 and a, b and c are integers <= n. =cut sub count_pythagorean_triplets ($n) { return 0 if $n < 5; my $count = 0; for my $a ( 1 .. $n ) { my $a2 = $a * $a; for my $b ( 1 .. $n ) { my $c2 = $a2 + $b * $b; my $c = int( sqrt($c2) ); if ( $c <= $n && $c * $c == $c2 ) { $count++; } } } return $count; }

Python Solution

ch-1.py

"""Perl Weekly Challenge 393 - Task 1: Pythagoras Multiplied.""" def count_pythagorean_triplets(n: int) -> int: """Count positive integer triplets (a, b, c) with a^2 + b^2 = c^2 and a, b, c <= n.""" if n < 5: return 0 count = 0 for a in range(1, n + 1): a2 = a * a for b in range(1, n + 1): c2 = a2 + b * b c = math.isqrt(c2) if c <= n and c * c == c2: count += 1 return count

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?

Example Output

Input: $str = "hello" Output: 9 (ASCII sum: 532, nearest prime: 523) Input: $str = "football" Output: 2 (ASCII sum: 841, nearest prime: 839) Input: $str = "a" Output: 0 (ASCII sum: 97 is prime)

Logic

First, sum the ASCII / ordinal values of all characters in the string. If the sum is already prime, the difference is 0. Otherwise, step outwards by diff = 1, 2, 3... checking whether sum - diff (if >= 2) or sum + diff is prime. Return the first diff that yields a prime.

Perl Solution

ch-2.pl

=head1 NAME ch-2.pl - Prime Step =head1 DESCRIPTION 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? =cut sub nearest_prime_diff ($str) { my $sum = sum0 map { ord($_) } split //, $str; return 0 if is_prime($sum); my $diff = 1; while (1) { if ( $sum - $diff >= 2 && is_prime( $sum - $diff ) ) { return $diff; } if ( is_prime( $sum + $diff ) ) { return $diff; } $diff++; } }

Python Solution

ch-2.py

"""Perl Weekly Challenge 393 - Task 2: Prime Step.""" def nearest_prime_diff(s: str) -> int: """Calculate absolute difference between ASCII sum of string and nearest prime.""" total_ascii = sum(ord(ch) for ch in s) if is_prime(total_ascii): return 0 diff = 1 while True: if total_ascii - diff >= 2 and is_prime(total_ascii - diff): return diff if is_prime(total_ascii + diff): return diff diff += 1