adamcrussell

Perl Weekly Challenge 022

Part 1

Sample Run

$ perl perl5/ch-1.pl
[5, 11][7, 13][11, 17][13, 19][17, 23][23, 29][31, 37][37, 43][41, 47][47, 53]

What I Did

There have been several prime number related challenges recently and I keep re-using much of the same code for them. The main parts of this re-used code downloads a list of pre-computed primes. I started this because computing them directly myself didn't seem that interesting but I had not done any old fashioned screen scraping in a while and so decided to take that route. Maybe next time I'll take a different approach now that this seems be getting a little tired. 

In any event, once the list of primes to be considered has been obtained they are compared in a nested loop which, in the interest of efficiency, has some exit criteria to end early. First, if we find all the pairs of sexy primes we want than that will cause the search to end. Secondly, there is no reason to search the list exhaustively since we are looking for primes with a difference of exactly six. Once we find that the differences are greater than six we can resume our search elsewhere. 

Part 2

Sample Run

$ perl perl5/ch-2.pl
64 97 113 96 31 96 257 96 98 96 99 261 258
Abra abracadabra

What I Did

I've never studied this particular algorithm before so this was a fun way to learn about it in depth. I found a nice article [1] which had pseudocode and examples and  was able to implement something straight away. My biggest surprise was realizing that the decoding process begins with a newly initialized table! For both encoding and decoding I have a single function which initializes a table (a hash) and the decoding table uses reverse to flip the key/values.

References

[1] LZW Data Compression


Comments for this post were locked by the author