• PWC 171 › Odd Abundant Numbers
    This week’s tasks include a simple number theory calculation, and a language feature. Here’s a look at task 1. Odd Abundant Numbers Abundant numbers are numbers where the sum of the proper divisors is greater than the number. The first odd abundant number is 945. 945’s proper divisors are 1, 3, 5, 7, 9, 15, … Read more
  • PWC 171 › First Class Functions
    Task #2 this week is no doubt about first class functions, but gets more specific, asking us to have a go at function composition: Create sub compose($f, $g) which takes in two parameters $f and $g as subroutine refs and returns subroutine ref i.e. compose($f, $g)->($x) = $f->($g->($x)) Task #2 Before we get too far ahead of ourselves, let’s briefly review what … Read more
  • PWC 170 › Primordial Numbers and Kronecker Products
    We’re back with our noses to the mathematical grindstone this week, with two straightforward tasks. Task 1 › Primordial Numbers The nth primordial number is simply the product of the first n primes. P(1) = 2, P(2) = 2×3 = 6, P(3) = 2x3x5 = 30, etc. P(0) is defined to be 1 as a … Read more
  • PWC 169 › Brilliant and Achilles Numbers
    This week, both tasks are quite short, so I’ll combine them into a single blog post. Task 1 › Brilliant Numbers Brilliant numbers are composite numbers with exactly two prime factors. Additionally, the prime factors must be the same length. My complete code is as follows:
  • PWC 168 › Perrin Primes
    Task #1 this week asks us to find the first 13 Perrin primes. “What’s a Perrin prime,” I can’t hear you asking? To answer that, we first have to look at the Perrin sequence, as described in OEIS A001608. It’s easy to generate: Starting with [3, 0, 2], each new term is determined by adding … Read more
  • PWC 168 › Home Prime
    Task #2 this week asks us to calculate a so called Home prime. Home primes are found by factoring a number and concatenating the prime factors (including powers, so 20 = 5×2×2), and repeating this until the result is a prime number. The given example, HP(10) can be found via the following steps: HP(10) = … Read more
  • PWC 167 › Circular Primes
    Task #1 this week has us finding circular primes, 3 digits or more. Circular primes are numbers that remain prime through all of their rotations. For example, since all the rotations of 113 (113, 131, and 311) are all primes, 113 is a circular prime. (131 and 311 are circular primes as well, but based … Read more
  • PWC 167 › Lanczos Gamma Approximation
    Let’s flex our mathematical legs a bit by implementing an approximation to the gamma function Γ(z). In this article, I’ll introduce what the gamma function is, what it’s for, and how to actually calculate it, using a well known approximation method. This topic leans more heavily into mathematics than usual for this blog. Some of … Read more
  • PWC 166 › K-Directory Diff
    Task #2 this week is another one of mine. The idea is to take any number of pathnames on the command line (and we must support at least three), and output a side-by-side diff of the files that are different. This is a skin-deep analysis, so there is no recursion, and no comparison of file … Read more
  • PWC 166 › Hexadecimal Words
    This is another one of my suggested tasks, and another time you’re going to read about a personal story that connects me to a task. For many years, right back to very first days I started hacking filesystems and writing kernel code, I’ve often needed to come up with 16- or 32-bit identifiers for things, … Read more
  • PWC 165 › Simple SVG generator
    The tasks this week are ones I devised. Allow me a moment to explain the motivation behind them. I often have a need to quickly visualize some random bits of data, and while I’ve gotten great mileage out of terminal output, and things like gnuplot or spreadsheet imports, sometimes better and more convenient results are … Read more
  • PWC 164 › Palindromic Primes
    The first task this week is to list all palindromic primes below 1000. Palindromes are very well known to Weekly Challenge enthusiasts as numbers (or words) that are the same forwards and backwards. Prime numbers should need no introduction. Prime Sieve I’m going to jump straight to it. Since the task is simple, I’m not … Read more
  • PWC 164 › Happy Numbers
    Task #2 this week comes to us from Robert DiCicco, and Robert demands happiness! Or at least numbers that are happy. Either way, I could use some right about now. We are to find the first 8 Happy Numbers in base 10. To test whether a number is Happy: replace the number by the sum … Read more
  • PWC 163 › A tail of two sums
    This week’s challenges are very quick, so I’m putting them in one blog post. The title is a bit of a play on words, given the tail recursion in part 2. Full solutions are available at GitHub. Task 1 › Bitwise Sum For the first task, we’re asked to first bitwise AND (&) each unique … Read more
  • PWC 162 › Wheatstone–Playfair Cipher
    Challenge #2 this week asks us to implement a version of the Playfair Cipher, which is a simple symmetric key cipher, invented in 1854 by Charles Wheatstone, that was used with pencil and paper. It ended up being called the Playfair Cipher because⁠—as I understand it⁠—the ironically named Lord Playfair promoted its use and basked … Read more
  • PWC 162 › ISBN-13
    This week‘s first task is simple: validate ISBN-13 codes. ISBNs have been around for a long time and come in many different flavours. ISBN-13 is a thirteen-digit code consisting of twelve digits plus a check digit. The checksum calculation is simple. Given the digits x1, x2, …, x12, and the check digit, x13: (10 – … Read more
  • PWC 161 › Pangrams
    For this week’s second challenge, the task is to generate a pangram. A pangram is simply a collection of words that contain every letter in the alphabet (English, in this case). A classic pangram example is: the quick brown fox jumped over the lazy dog Wikipedia
  • PWC 161 › Abecedarian Words
    I know, it’s been a long time! This week, I’m back. I submitted the challenges for this week, so I thought it’s only fair that I give them a go myself. Unfortunately, I realized too late that this Abecedarian Words is essentially the same as Week 111’s “Ordered Letters” submitted by the venerable E. Choroba. … Read more
  • PWC 110 › Phone Number Validation
    Personal note: I’ve had to take a break from participating in the PWC, but I’m back for this week, at least. Hopefully I’ll be able to contribute more again. The first task this week is a sort of simple phone number validation, based on provided templates. Numbers must match the following, where n is any … Read more
  • PWC 110 › Transpose CSV File
    The second task this week is simple: given a (simplified) comma-separated-value (CSV) file, transpose its rows and columns. For example: The challenge task does not actually refer to the input as CSV, so I’m using that term loosely, with simplified parsing to match the input specification. If more compliant parsing is needed, one could use … Read more
  • PWC 056 › Path Sum
    Task #2 this week is a simple tree traversal: You are given a binary tree and a sum, write a script to find if the tree has a path such that adding up all the values along the path equals the given sum. Only complete paths (from root to leaf node) may be considered for … Read more
  • PWC 056 › Diff-K
    Task #1 this week is to implement the Diff-K algorithm, as explained by Mohammad: You are given an array @N of positive integers (sorted) and another non negative integer k. Write a script to find if there exists 2 indices i and j such that A[i] – A[j] = k and i != j. This one is pretty easy. We can boil down the solution … Read more
  • PWC 054 › Collatz Conjecture
    Task #2 this week was one of my devising: It is thought that the following sequence will always reach 1: For example, if we start at 23, we get the following sequence: 23 → 70 → 35 → 106 → 53 → 160 → 80 → 40 → 20 → 10 → 5 → 16 → … Read more
  • PWC 054 › kth Permutation
    Task #1 this week is as follows: Write a script to accept two integers n (>=1) and k (>=1). It should print the kth permutation of n integers. For more information, please follow the wiki page. For example, for n=3 and k=4, the possible permutation sequences are 123, 132, 213, 231, 312, 321. The script should print the 4th permutation sequence, 231. This is fairly straightforward. There are … Read more
  • PWC 053 › Vowel Strings
    Task #2 this week has us construct “vowel strings,” as described by Mohammad: Write a script to accept an integer 1 ≤ N ≤ 5 that would print all possible strings of size N formed by using only vowels (a, e, i, o, u). The string should follow the following rules: ‘a’ can only be followed by ‘e’ and ‘i’. ‘e’ can only be … Read more
  • PWC 053 › Matrix Rotation
    Task #1 this week is as follows: Write a script to rotate the following matrix by given 90/180/270 degrees clockwise. At first glance, I thought this was a simple matrix transpose, which is what you get when you swap the rows and columns of a matrix. The transposition (T) of the example matrix would give us:
  • PWC 052 › Lucky Winner
    Task #2 this week can be as simple or as difficult as you make it. Mohammad’s description: Suppose there are following coins arranged on a table in a line in random order. £1, 50p, 1p, 10p, 5p, 20p, £2, 2p Suppose you are playing against the computer. Player can only pick one coin at a … Read more
  • PWC 052 › Stepping Numbers
    Task #1 this week is straightforward. Here’s what Mohammad had to say about it: Write a script to accept two numbers between 100 and 999. It should then print all Stepping Numbers between them. A number is called a stepping number if the adjacent digits have a difference of 1. For example, 456 is a stepping number … Read more
  • PWC 051 › 3Sum and Colourful Numbers
    Personal note: It’s been an extremely challenging couple of weeks for me, due to a family emergency. As such I’m combining my solutions into a single, shorter blog post this week. If you also follow my review posts on the site, you’ll note they are quite late as well. I’m sorry about that! Hopefully … Read more
  • PWC 050 › Noble Integers
    Task #2 this week is described as follows: You are given a list, @L, of three or more random integers between 1 and 50. A Noble Integer is an integer N in @L, such that there are exactly N integers greater than N in @L. Output any Noble Integer found in @L, or an empty list if none were found. An interesting question is whether … Read more
  • PWC 050 › Merge Intervals
    Task #1 this week asks us to merge integer ranges. Here is the task description: Write a script to merge the given intervals where ever possible. [2,7], [3,9], [10,12], [15,19], [18,22] The script should merge [2, 7] and [3, 9] together to return [2, 9]. Similarly it should also merge [15, 19] and [18, 22] together to return [15, 22]. The final result should be … Read more
  • PWC 049 › LRU Cache
    Task #2 this week has us build an LRU cache, which is a cache of fixed capacity, whereby when it is full but a new item needs to be added, the item that was least-recently accessed is evicted from the cache. For example, if a cache of capacity = 3 contains [a b c] (where … Read more
  • PWC 049 › Smallest multiple containing only 1 and 0
    Task #1 this week is as follows: Write a script to accept a positive number as command line argument and print the smallest multiple of the given number consists of digits 0 and 1. Brute Force It’s easy enough to brute force this problem, by checking every multiple in order: $_ now contains the answer. Unfortunately, some numbers—particularly … Read more
  • PWC 048 › Survivor (Josepheus problem)
    Task #1 this week is described as follows: There are 50 people standing in a circle in position 1 to 50. The person standing at position 1 has a sword. He kills the next person i.e. standing at position 2 and pass on the sword to the immediate next i.e. person standing at position 3. … Read more
  • PWC 048 › Palindrome Dates (mm/dd/yyyy)
    Task #2 this week asks us to (and I quote): print all Palindrome Dates between 2000 and 2999. The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001. It’s pretty easy to avoid using any sort of date library with a couple of key observations about the problem domain. First, we’ll split … Read more
  • PWC 047 › Roman Calculator
    The first challenge this week tasks us with evaluating arithmetic operations using Roman numerals. The description seems to indicate that only one operation will be given (for example, II + IV), but I have elected to support arbitrary arithmetic expressions. First, I needed a way to convert to and from Roman and Arabic numerals. I … Read more
  • PWC 047 › Gapful Numbers
    Task 2 this week has us print the first 20 “gapful numbers,” as described by OEIS sequence A108343. Gapful numbers are numbers greater than 99 that are evenly divisible by their first and last digits combined. For example, 132 is a gapful number because 132 ÷ 12 = 11. This is certainly the easier of … Read more
  • PWC 046 › Cryptic Message
    Challenge #1 this week is the following: The communication system of an office is broken and message received are not completely reliable. To send message Hello, it ended up sending these following:
  • PWC 046 › Is the Room Open? (500 Doors)
    The second of the challenges this week poses the following question (paraphrased): Suppose we have 500 doors, and 500 employees. The first employee opens all the doors. The second employee closes every 2nd door (doors 2, 4, 6, … 500). The third employee closes every third door if it is open, or opens it if … Read more
  • Quine
    Challenge #2 this week asks for “a script that dumps its own source code”. This is almost a quine, although Mohammad did not name it as such. Specifically, we are given the constraint that perl | diff – must return nothing. What’s a Quine? A quine, otherwise known as a self-replicating program, is … Read more
  • Square Secret Code
    Task #1 this week is a simple cipher, described as follows: The square secret code mechanism first removes any space from the original message. Then it lays down the message in a row of 8 columns. The coded message is then obtained by reading down the columns going left to right. Given “The quick brown … Read more
  • Make it $200
    For challenge #2 this week, the task is to start with $1, and by either adding $1 or doubling the amount, reach $200 in the smallest possible number of steps. “Greedy never works” In a 75-minute lecture some decades ago, my Advanced Algorithms professor said, over and over, “greedy never works,” while all the while … Read more
  • Only 100, please
    Challenge #1 this week is as follows: You are given a string “123456789”. Write a script that would insert ”+” or ”-” in between digits so that when you evaluate, the result should be 100. I am going to add the additional constraint that we want all possible solutions, because that is a much stronger statement, and not that difficult to … Read more
  • Self-descriptive Numbers
    Challenge #2 this week (43) is to generate Self-descriptive Numbers in arbitrary bases. A self-descriptive number, as described by Wikipedia, is an integer m that in a given base b is b digits long in which each digit d at position n (the most significant digit being at position 0 and the least significant at position b – 1) counts how many instances of digit n are … Read more
  • Olympic Rings
    Challenge #1 this week (43) is a number puzzle. In short, the task is to fill in the numbers 1, 2, 3, 4, and 6 into the spaces within the intersecting Olympic rings, so that the numbers in each ring sum to 11. Some of the spaces are already filled in (given). Here is the … Read more
  • Balanced Parentheses
    The second challenge this week is another old chestnut: Write a script to generate a string with random number of ( and ) brackets. Then make the script validate the string if it has balanced brackets. The question has two parts:
  • Octal Representation
    Challenge 1 this week is an easy one: Write a script to print decimal number 0 to 50 in [the] Octal Number System. Perl and Raku We can solve this with the following polyglot (runs in both languages at once): Still, in Raku, we can do the following:
  • Leonardo Numbers
    Happy new year! We are on Week 41, and this is Challenge #2. The Leonardo Numbers (A001595) are a simple recursively defined sequence: The sequence starts: 1, 1, 3, 5, 9, 15, 25, 41, 67, 109, 177, 287, 465, 753, 1219, 1973, 3193, 5167, 8361, 13529, 21891, … You’ll note this sequence is very similar … Read more
  • Attractive Numbers
    This week, Mohammad asks us to output a list of all Attractive Numbers between 1 and 50. Attractive numbers, as described by the Online Encyclopedia of Integer Sequences (OEIS) are: Numbers with a prime number of prime divisors (counted with multiplicity) OEIS Sequence A063989 The first numbers are 4, 6, 8, 9, 10, 12, 14, … Read more
  • Zip6
    The zip6 function has long been a staple of the List::MoreUtils CPAN module. The Week 40 challenge #1 describes it very well. But even more succinctly: zip6 takes an array of arrays (AoA) and returns another AoA of all the 1st elements, then the 2nd, and so on. It’s not a difficult algorithm by any … Read more
  • Reverse Polish Notation
    This is my first blog post regarding the Perl Weekly Challenge tirelessly maintained by Mohammad Anwar. Although I’ve been doing the challenge for a while now, I haven’t maintained my blog for years. Let’s change that! This week’s challenge #2 from the Perl Weekly Challenge is a computer science classic: create a Reverse Polish Notation … Read more