Happy New Year! At the start of this year, I remembered a challenge that Dyalog had come up with back in 2016 that was focused around the year 2016. So, I decided to see how I would get on trying the same challenge with the year 2021.
The challenge is to write a series of expressions using the year (in this case 2021) to return each number from 0-100, meaning an expression per number. Each expression must contain the digits of the year in order and include no other digits. The digits from the year can be grouped together or separated using spaces, decimals or any functions or operators you wish to use, for example: 2021, 20+21, 2.021, 2 0 2 1.
The original challenge was intended for users of APL, so the official Dyalog rules are specifically tailored to APL solutions, but do not let this put you off trying it in different languages. I would say the only thing that needs to be followed strictly for non-APL users is that the numbers are in order, numbers are only separated by spaces, decimals or any other function or operator, and that other digits are not included.
My expressions for 0 - 10
As I am an APL developer, I decided to tackle the challenge using APL. I will take you through my expressions to produce 0-10 with explanations of what they are doing.
This is a straightforward logical evaluation, does 20 equal 21, the answer being no, returns a Boolean equivalent to no/false being 0.
Using the symbol Downstile dyadically (with a left and right argument) means return the smallest of the 2 arguments, so the above expression means which is smaller between 202 and 1, so returns 1.
Again, using the symbol Downstile dyadically, so the above expression means which is smaller between 2 and 021, so returns 2.
Again, using the symbol Downstile dyadically, but this time we are also using a plus, so the above expression evaluates 2+1 first and then evaluates which is smaller between 20 and 3, so returns 3.
The order of execution in APL, in its simplest form, is evaluate from right to left and evaluate parentheses first. More information on order of execution in APL can be found in one of my previous blogs.
Using the symbol Upstile dyadically means return the largest of the 2 arguments, so the right-hand side of the above expression (
02⌈1 ) means which is larger between 02 or 1, so returns 2. We are then left with the left-hand side of the expression (
2+ ) along with the already evaluated right-hand side meaning we have
2+2, which returns 4.
In this expression we simply add everything together to get 5.
Keeping in mind the order of execution in APL, we operate the right-hand side first (
02+1 ) giving us 3, we are then left with
2×3 giving us 6.
2+1 first, gives us 3, then operating
20÷3, gives us 6.666…, then applying Upstile monadically (only with a right argument) acts as round up to the next whole integer, so returns 7.
02+1 first gives us 3, we then operate
2*3, which means 2 to the power of 3, which gives us 8.
02+1 first gives us 3, then applying Upstile gives us the larger of 2 and 3, which is 3. We then use the operator commute with the function Times
(×⍨) which means multiply the right argument by itself, so we are effectively operating
3×3 so we get 9.
Using Upstile, we find the greater of 2 and 1, which gives 2, we then do
20÷2, which returns 10.
How many can you get?
Now I have gone through my first set of expressions, why not see how many of the 0-100 you can get and in what languages, please let me know in the comments. Also, for any APLers out there that can see alternatives or improvements to my expressions, please let me know.
Sam is an APL Developer at Optima Systems. He started at Optima in 2012 as an apprentice straight after finishing College. Sam decided to take Computing whilst studying at college, yet, he never intended to but, he thought it sounded interesting at the time. Sam’s original career plans involved studying Meteorology at university, to become a Meteorologist. His plans changed when he realised how much he enjoyed code. More about Sam.