Perl Weekly Challenge 393.
My solutions (task 1 and task 2 ) to the The Weekly Challenge - 393.
Task 1: Pythagoras Multiplied
Submitted by: Ulrich Rieke
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.
Example 1
Input: $n = 20
Output: 12
(3,4,5), (4,3,5), (5,12,13),(6,8,10),
(8,6,10), (8,15,17), (9,12,15),(12,5,13),
(12,9,15),(12,16,20),(15,8,17),(16,12,20)
Example 2
Input: $n = 7
Output: 2
(3,4,5),(4,3,5)
Example 3
Input: $n = 1
Output: 0
Example 4
Input: $n = 15
Output: 8
Example 5
Input: $n = 30
Output: 22
One can build all Pythagorean triplets starting from the
square of any complex number z=x+iy with integer
coordinates x and y. Its square is z²=x²-y²+2i(xy)
with magnitude |z²|=√((x²-y²)²+4x²y²)=(x²+y²), so that
(x²-y², 2xy, x²+y²) form a Pythagorean triplet. The others may
be formed by permutation and by integer multiplication.
We consider all possible numbers x from 2
up to √n and all numbers y from 1 up to the smallest
among x-1 or n-x² that are relative primes to x, i.e.,
their greatest common divisor is 1, and I add the number of
multiples of the resulting triplet n/(x²+y²) to the running
total. Finally, I multiply by 2 to account for
permutations. The result fits in a 2-liner.
Examples:
perl -MPOSIX=floor,fmin -MMath::Prime::Util=gcd -E '
for(@ARGV){say "$_ -> ",f($_)}sub f($n){$t=0;for my $x(2..sqrt$n){for my $y(1..fmin($x-1,
$n-$x**2)){next unless gcd($x**2-$y**2,2*$x*$y)==1;$t+=floor(($n)/($x**2+$y**2))}}2*$t;}
' 20 7 1 15 30
Results:
20 -> 12
7 -> 2
1 -> 0
15 -> 8
30 -> 22
The full code is
1 # Perl weekly challenge 393
2 # Task 1: Pythagoras Multiplied
3 #
4 # See https://wlmb.github.io/2026/09/28/PWC393/#task-1-pythagoras-multiplied
5 use v5.40;
6 use Math::Prime::Util qw(gcd);
7 use POSIX qw(fmin);
8 die <<~"FIN" unless @ARGV;
9 Usage: $0 N0 N1...
10 to find how many Pythagorean triplets can be formed with
11 positive numbers up to Ni
12 FIN
13 for(@ARGV){
14 say "$_ -> ", pythagorean($_);
15 }
16 sub pythagorean($n){ # count pythagorean triplets
17 my $t=0;
18 for my $x(2..sqrt $n){
19 for my $y(1..fmin($x-1, $n-$x**2)){
20 next unless gcd($x**2-$y**2,2*$x*$y)==1; # check relative primes
21 $t += floor(($n)/($x**2+$y**2)); # count multiples
22 }
23 }
24 return 2*$t; # add permutations
25 }
Example:
./ch-1.pl 20 7 1 15 30
Results:
20 -> 12
7 -> 2
1 -> 0
15 -> 8
30 -> 22
Task 2: Prime Step
Submitted by: Ulrich Rieke
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 1
Input: $str = "hello"
Output: 9
The ordinal values of "hello" are [104,101,108,108,111],
summing up to 532. The nearest prime number to 532 is 523,
resulting in an absolute difference of 9.
Example 2
Input: $str = "football"
Output: 2
Starting with the values [102,111,111,116,98,97,108,108] and
the sum 841. We find 839 as the nearest prime number, so the
difference is 2.
Example 3
Input: $str = "a"
Output: 0
Example 4
Input: $str = "challenge"
Output: 2
The ordinal values of "challenge" are [99, 104, 97, 108,
108, 101, 110, 103, 101], which sum up to 931.
The nearest prime number to 931 is 929, so the difference
is 2.
Example 5
Input: $str = "perl"
Output: 2
The ordinal values of "perl" are [112, 101, 114, 108],
summing up to 435. Nearest prime is 433, so the difference
is 2.
I simply split the input string, find the ord of all
characters and sum0 them, test the result with is_prime,
in which case, the result is 0, and otherwise, compute the
next_prime and the prev_prime and choose the closest to
get the minimum difference. The result fits a 1.5-liner.
Examples:
perl -MMath::Prime::Util=next_prime,prev_prime,is_prime -MList::Util=min,sum0 -E '
for(@ARGV){$s=sum0 map {ord} split"";say "$_ -> ",is_prime $s?0:min$s-prev_prime($s),
next_prime($s)-$s}
' hello football a challenge perl
Results:
hello -> 9
football -> 2
a -> 0
challenge -> 2
perl -> 2
The full code is:
1 # Perl weekly challenge 393
2 # Task 2: Prime Step
3 #
4 # See https://wlmb.github.io/2026/09/28/PWC393/#task-2-prime-step
5 use v5.36;
6 use Math::Prime::Util qw(next_prime prev_prime is_prime);
7 use List::Util qw(min sum0);
8 die <<~"FIN" unless @ARGV;
9 Usage: $0 S0 S1...
10 to find the distance between the sum of the ordinal
11 values of the string Si and the closest prime number.
12 FIN
13 for(@ARGV){
14 my $sum = sum0 map {ord} split"";
15 say "$_ -> ",
16 is_prime $sum?
17 0 :
18 min $sum - prev_prime($sum), next_prime($sum)-$sum
19 }
Examples:
./ch-2.pl hello football a challenge perl
Results:
hello -> 9
football -> 2
a -> 0
challenge -> 2
perl -> 2
/;