Sum of Individuals Gives Meaning - CY's Take on PWC#076

If you want to challenge yourself on programming, especially on Perl and/or Raku, go to https://perlweeklychallenge.org, code the latest challenges, submit codes on-time (by GitHub or email).

---

Task 1 Prime Sum

What a dish for math geeks! Goldbach's conjecture is immediately recalled. I have chosen to print the answer first, than give the expression and explanation.

$ perl ch-1.pl 1
0
as 1 is smaller than the smallest prime.

$ perl ch-1.pl 4
2
as 2 + 2 = 4 and 4 is not a prime.

$ perl ch-1.pl 9
2
as 2 + 7 = 9
and 9 is not a prime.

$ perl ch-1.pl 200001
2
as 2 + 199999 = 200001
and 200001 is not a prime.

$ perl ch-1.pl 200003
1
as 200003 is a prime.

$ perl ch-1.pl 200007
3
3 + 69821 + 130183 = 200007 .
The answer can't be 1 or 2 because neither 200005 nor 200007 is a prime; and
 there are no even primes except 2
 (two odd primes sum to an even number).

When it comes to even number inputs or answer=3 inputs, the performance of the script runs a bit inconsistently because I saves the primes as hash keys of a hash.

People never heard of Goldbach's conjecture suffer. This makes me think of a task of PWC#008, Task 1 Perfect numbers(task statement, Recap, Perl Review), in which knowledge of Mersenne primes is needed for a script completing effectively.

Task 2 Word Search

I must do more coding on two-dimensional problems similar to this task because I spent roughly 80% of coding time to debug with the diagonals.

Confusing with the "array of arrays"/"array of references pointing to arrays", though, I have chosen to represent each element on the text grid by ${$matrix[$row_number]}[$col_number].

Lines of rows, columns, diagonals and antidiagonals (and their reverses) are generated and then check if any existence of words through regular expressions.

For the antidiagonals, as I had encountered too much hardship on generating the diagonals, I used a dummy matrix - a vertical reflection of the original matrix, and got all of its diagonals - which are antidiagonals of the original matrix.

@diagonal = find_diagonal(\@matrix);
my @newmatrix = vertical_reflection(\@matrix) ;
my @antidiagonal = find_diagonal( \@newmatrix);

I read that some other team members looping over each element of grid and hunting for words which start with that alphabet. Is the performance better in that way? I am not sure; not going to test the codes, because the running time seems not consuming enough (less than 1 min). #1.

It would be interesting if a very large grid was given (as bonus?) and requested coders use their preferred ways to optimize. There are only 26 alphabets in English, and I guess it should be interesting when the grid is REALLY large. Before coding the task, I thought whether I was going to use the vowels (there must be at least one vowel inside a typical English word and the vowel appears together only for ee, oo, ea, ai, ie, oa, oe, oi, ou and ue typically). (Part of my name "Cheok" is also an exception, a rare exception#2, of course... ), or other techniques like checking the set of alphabets on a segment, in order to optimize. I have used a word list of just 5000 words from online as my Linux Mint has no local dictionaries. Anyway now the script performs satisfactorily (and my laptop doesn't need to run a 40-hour execution).

Other Programming Languages

I get some time tonight and tomorrow morning, therefore thoughts of coding in Java or Python for Task 1 appear; but I chose to improvise my blogpost, and study a bit about assembly language (because I am trying to understand TAOCP, and it describes an assembly language "MIX") or read books on classical computer science tasks (one of them is The New Turing Omnibus). I have a break from my "self challenge" - Task 1 is too brief and Task 2 is too code-intensive. BUT I LIKE FINISHING THEM WITH PERL.

Remarks

#1: "In short, there is a trade-off between how measurable something is, and how meaningful it is likely to be in terms of actual behaviour." (I am reading x+y: A Mathematician's Manifesto for Rethinking Gender, by Eugenia Cheng, published in July 2020.) The title of this blogpost is partly inspired by the book. Interestingly, this week I finally told our PWC organiser manwar should refer me as she/her on twitter - a "bug" I had discovered on the Perl Weekly...
#2: My elder sister was born in Macau (the small region besides Hong Kong and previously ruled by Portugal). The births and deaths register office there made the "Cheok" for her. After 5 years, my name had to follow suit. Unfortunately I had spelt my name with "Choek" for about 3~5 years in the beginning of primary school.

Aftermath

As said, I had a hard time with Task 2. I could make the codes related to the diagonals run without warning by using grep {defined $_} @_ in a subroutine joinline but I decided to pick out the bug. And I made it this afternoon! Lesson: Try your best not to allow any bugs or warnings present in codes; don't give up. (Spending such a long time on debugging, I decide to add back "beginner in coding" in my short description...)


Do tell or correct me, if you have oppositions, want to discuss or give me advice!
Besides email or commenting, now you may contact me on Twitter.

link for codes: ch-1.pl, ch-2.pl

Stay alert and healthy! □

Leave a comment

About C.-Y. Fung

user-pic a beginner in programming; passionate in mathematics, distance running and gender issues; from Hong Kong. https://twitter.com/e7_87