Peter’s blog ✴ Week 393 ✴ 28 September 2026
THE WEEKLY CHALLENGE
All about primes
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've used Math::Prime::Util for this as I've submitted Sieve
of Eratosthenes solutions to at least 11 earlier challanges.
The summation of the Unicode code points - ie what ord($a)
returns - is an easy single line.
It's then just a case of stepping up and down from that sum to find the nearest prime in both directions, and choosing the closer.
I could of course have stopped as soon as I found a prime in either direction, but it seems more satisfying to check both.
Peter has come up with an efficient and performance-oriented solution for Task 1 of Perl Weekly Challenge 393. He precomputes a square lookup table and symmetry bounds (a < b) that makes it possible for him to get rid of costly square root and integer operations with the help of precomputation.
This review may cover either or both challenges for this week.
#!/usr/bin/perl # Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge use v5.26; # The Weekly Challenge - 2026-09-28 use utf8; # Week 393 - task 2 - Prime step use warnings; # Peter Campbell Smith binmode STDOUT, ':utf8'; use Math::Prime::Util 'is_prime'; use Encode; prime_step('football'); prime_step('hello'); prime_step('challenge'); prime_step('supercalifragilisticexpialidocious'); prime_step('ⱠⱡⱢⱣⱤⱥⱦ'); sub prime_step { my ($string, $answer, $j, $sum, $explain, $above, $below); $string = $_[0]; $answer = -1; $below = $above = 0; # sum the ordinal values of the string $sum += ord($_) for split(//, $string); # work up and down until we find a prime for $j (0 .. 1000) { $below = $j if is_prime($sum - $j); $above = $j if is_prime($sum + $j); # found surrounding primes if ($below and $above) { $answer = ($below < $above) ? $below : $above; $explain = qq[sum = $sum: primes below and above = ] . ($sum - $below) . ', ' . ($sum + $above); last; } } say qq[\nInput: '$string']; say qq[Output: $answer\n $explain]; }
16 lines of code
Input: 'football' Output: 12 sum = 851: primes below and above = 839, 863 Input: 'hello' Output: 9 sum = 532: primes below and above = 523, 541 Input: 'challenge' Output: 2 sum = 931: primes below and above = 929, 937 Input: 'supercalifragilisticexpialidocious' Output: 12 sum = 3643: primes below and above = 3631, 3659 Input: 'ⱠⱡⱢⱣⱤⱥⱦ' Output: 4 sum = 79541: primes below and above = 79537, 79549
Any content of this website which has been created by Peter Campbell Smith is in the public domain